String.Format

does anyone know if this works in javascript or does it only work in c#?

i’m trying to pad numbers with zeros, and any code i’ve grabbed from googling doesn’t seem to work. unity doesn’t throw any errors at me - it just isn’t formatting anything, just displaying the format code i’ve been trying.

number.ToString(“0000”) works well for turning a number into a string and padding it with zeroes. If number is 12, for example, the string would be 0012. This works in Javascript as well as C#, I believe.

2 Likes

awesome, it works great. thanks ; )

String.Format should also work fine. Do you have an example of code using String.Format you are having problems with? If you post it here, I could take a look at it to see what’s going wrong.

There are a few examples of code using String.Format in the Unify Wiki

I always use .ToString (which is probably the same function, or calls the same function)

hours.ToString(“#0”) + “:” + minutes.ToString(“00”) + seconds.ToString(“00.00”)

String.Format is bit more effective than concatenating strings using ToString and the + operator. Every time you use the + to concatenate 2 strings, the runtime has to allocate a new string and copy the two strings into that. Statements like s1 + s2 + s3 will first allocate a new string for the result of s1 + s2 and then another one for (s1 + s2) + s3.

String.Format only allocates a single string for the output and copies all the input parameters into that one. Also having the format string at the beginning separates the format from the data, potentially making your code more readable

i was trying to figure out if it worked in javascript. i didn’t bookmark anything but the last line i tried that i had left commented out in my script is:

ammoC = String.Format(“%4.3d”, ammoCount);

i don’t remember what else i tried (it was before the holidays - its all a blur now ; ) but it was all somewhat similar to that.

It’s seems you are tring to use C printf format strings. The format strings for String.Format are different. See the documentation on String.Format on msdn for more information.

“%4.3d” in your example should be “{0:####.000}”

yeah, that above code was just plain wrong on my part. thanks to your help i found what i needed to pad my number with zeros.

i have one last question regarding this. in my specific case with this string i am only formatting that one parameter - so would it not be better to use NCarter’s method here as it uses less code? (this of course is presuming i coded String.Format efficiently - not a safe bet ; )

the two versions:

function UpdateGUI ()
{
	if (weaponScript)
	{
		ammoCount = weaponScript.GetBulletsLeft();
		ammoC = String.Format("{0:000}", ammoCount);
		ammoGUI.text = ammoC.ToString();
	}
}
function UpdateGUI ()
{
	if (weaponScript)
	{
		ammoGUI.text = weaponScript.GetBulletsLeft().ToString("000");
	}
}

little practical difference (although the .ToString() in the first does nothing). Just use whatever you like the most.

ok great, thanks guys : )