- Home /
Seperate large numbers with comma
How do I format a number such that it has a comma in every thousand position (both integers and floating values?)
For example 1000000.35 will be 1000,000.35?
Answer by Lipis · Mar 12, 2010 at 12:21 PM
You can use the ToString()
function of the variable like this:
var num : float = 1000000.35;
Debug.Log(num.ToString("n2"));
Where n
is for number
and 2 is the number of decimal places you want to display. If you don't want to use the comma use f
(float
) instead.
Answer by duck · Mar 12, 2010 at 12:46 PM
To complement Lipis' answer:
The version of Mono (which is a version of .net) in Unity comes with a wealth of built-in formatting options, so you can use the MSDN reference to look this sort of thing up:
For more information see the MSDN documentation on "N" formatting:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#NFormatString
And on standard number formatting in general:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
And on applying custom formats to your number strings:
http://msdn.microsoft.com/en-us/library/0c899ak8%28v=VS.90%29.aspx
an incredibly useful tip, thanks.
higheScore.ToString( "n0" )
it's that easy. $$anonymous$$any Unity programmers are not familiar with the NET libraries, so again it's a hugely useful tip!
Thank you guys for the answers, especially you (duck) with the links that break down what everything means. Very very helpful indeed, just what I was looking for.
Your answer
Follow this Question
Related Questions
FormatException: Input string was not in the correct format 1 Answer
Formatting Date and Time 3 Answers
Litjson autoformat 1 Answer
Currency String Format 1 Answer
Can't Convert string binary to long. 2 Answers