- Home /
How to format numbers for print()... is javascript different to C?
Hi
I couldn't find anything in the docs about using the print() function. I googled for a javascript reference which said that javascript print() uses the same syntax as for C printf(), but that doesn't seem to work in Unity. Can anyone point me to some docs for numeric precision syntax (%.2f etc.)?
In a nutshell, how would I format this C statement for Unity?
printf("%s is %d and thinks pi = %.2f", "Dave", 42, 3.14159);
( Should print: Dave is 42 and thinks pi = 3.14)
Thanks
Steve
I think if you write number.ToString(2f) it'll turn the number into a string with 2 decimal places
Thanks for the suggestion, but it still gives a syntac error.
Answer by Mike 3 · Jul 09, 2010 at 12:25 PM
print only takes a single string. If you want more printf style input, use String.Format like this:
print( String.Format("{0} is {1} and thinks pi = {2:F2}", "Dave", 42, 3.14159) );
Documented here: http://msdn.microsoft.com/en-us/library/b1csw23d.aspx
And info on numeric formatting for it here: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Thanks so much, exactly what I needed. Would never have thought of looking on msdn.
Fun hmm? Unity's javascript is basically just a javacript-ish .NET implementation, so it has full access to the majority of .NET
Good to know, I'll have a root around see what else I can find.
The entire $$anonymous$$SDN Library is why I use C# and not JavaScript