Number Formatting in Unity: Useful C# ToString Formats
When you make a game, there may come a time when you need to display some numbers on the screen. You can’t just print out the whole thing with super long decimal points. You need a way to format it. Fortunately, Unity, or rather C#, has a way to make numbers look prettier on screen.
The easiest way to display a float value as a string with a specific number of decimal places is to give ToString an “F” specifier followed by your desired number of decimal places. By setting the specifier to “F2”, i.e. ToString(“F2”), the resulting string will have fixed decimal places of 2.
The example above is one of many short formatting specifiers available in C#. I’m going to show you some of the useful ones below so make sure to keep on reading!
Table of Contents
- How To Limit Decimal Places Of A Number In Unity?
- How To NOT Show Decimal Digits When It’s A Whole Number
- How Do You Add Leading Zeroes To A Number In Unity?
- How To Add Literal Strings To The String Format?
- Conclusion
How To Limit Decimal Places Of A Number In Unity?
The easiest and quickest way to limit the decimal places of a number is to give an “F” specifier to the ToString() function.
private float num = 123.456f;
num.ToString("F2");
// Result: 123.46
Code language: C# (cs)That is it. That’s the quickest way to achieve the desired decimal places in Unity.
And as you might have noticed, the value is rounded automatically to the nearest digit preceding the decimal. So the number between 5 and above will be rounded up. Anything below 5 will be rounded down.
float numA = 12.345f;
numA.ToString("F2");
// Result: 12.35
float numB = 99.996f;
numB.ToString("F2");
// Result: 100.00
float numC = 99.994f;
numC.ToString("F2");
// Result: 99.99
float numD = 200.123456f;
numD.ToString("F3");
// Result: 200.123
Code language: C# (cs)“But what if I don’t want the number to be rounded up?”
If you don’t want this behavior, then you must manually truncate the number before printing it.
float num = 100.439f;
float truncatedNum = System.Math.Truncate(num * 100) / 100;
truncatedNum.ToString();
// Result: 100.43
Code language: C# (cs)Let me explain the code above a bit in detail.
float truncatedNum = System.Math.Truncate(num * 100) / 100;Code language: C# (cs)This line is where the magic happens. What System.Math.Truncate() does is it takes a number and returns only the integral part (the numbers on the left side of the dot) of that number.
For example, System.Math.Truncate(123.456f) will return only the 123 part.
Important: This is not to be confused with the Mathf class of Unity Engine library. System.Math is the C#’s math library which doesn’t contain the Truncate method.
The reason we multiply the number by 100 is to keep the decimal digits we want. In this case, we want up to the 2nd decimal digit, hence why we multiply it by 100.
Once the unwanted part is gone, we then divide the value by 100 to push it back.
float num = 123.456789f; // <-- Let's say we want the 789 removed
// num = 123.456789
float shiftedNum = num * 1000; // Shift the value by 123
// shiftedNum = 123456.789
float truncatedNum = System.Math.Truncate(shiftedNum); // Truncate it
// truncatedNum = 123456
float num = truncatedNum / 1000; // Shift the value back
// num = 123.456
Code language: C# (cs)That is it. It’s a bit of a roundabout way to do this, but it works!
How To NOT Show Decimal Digits When It’s A Whole Number
“What if I don’t want to display ‘.00’?” I hear you ask.
To hide decimal digits when the value is a whole number, use “#.###” as an argument in the ToString() function to specify that decimal digits are optional when they are not significant digits. The “#” after the decimal symbol indicates the maximum decimal digits to show.
Here are some examples:
float num = 100f;
num.ToString("F4");
// Result: 100.0000
num.ToString("#.#");
// Result: 100
num.ToString("#.####");
// Result: 100
// ---------------------
float num2 = 100.573f;
num2.ToString("F1");
// Result: 100.6
num2.ToString("#.#");
// Result: 100.6
num2.ToString("#.##");
// Result: 100.57
num2.ToString("#.#######");
// Result: 100.573
// It will not be displayed as 100.5730000 because the zeroes after are non-significant.
Code language: C# (cs)On the other hand, there is also a way to make it always show at least x decimal digits by combining “#” with “0”.
float num = 100f;
num.ToString("F2");
// Result: 100.00
num.ToString("#.00");
// Result: 100.00
Code language: C# (cs)If there is no “#” it will behave exactly like the “F” specifier where it will display exactly the specified number of decimal digits. In this case, 2 decimal digits.
float num2 = 55.1f;
num2.ToString("F5");
// Result: 55.10000
num2.ToString("#.00#");
// Result: 55.10
num2.ToString("#.000");
// Result: 55.100
num2.ToString("#.00#####");
// Result: 55.10
Code language: C# (cs)float num3 = 300.123456f;
num3.ToString("F3");
// Result: 300.123
num3.ToString("#.00#");
// Result: 300.123
num3.ToString("#.0000000");
// Result: 300.1234560
num3.ToString("#.000#######");
// Result: 300.123456
Code language: C# (cs)How Do You Add Leading Zeroes To A Number In Unity?
Adding leading zeroes is similar to “displaying at least x decimal digits” above, but instead of putting “0” specifiers after the dot, we put them in front instead.
float num = 25.335f;
num.ToString("00#.00");
// Result: 025.34
float num2 = 0.27f;
num.ToString("#.###");
// Result: .27
num.ToString("0.###");
// Result: 0.27
num.ToString("000.000");
// Result: 000.270
Code language: C# (cs)The zeroes will be replaced by the numbers at the corresponding digits.
And unlike the “#” specifier, the “0” specifier will always try to display a value even if the value is 0.
float num = 0f;
num.ToString("#");
// Result: (Nothing, it's an empty string.)
num.ToString("0");
// Result: 0
num.ToString("0.00");
// Result: 0.00
num.ToString("0.#");
// Result: 0
Code language: C# (cs)How To Add Literal Strings To The String Format?
Displaying a literal character string can be done by simply putting the quotation marks around the string, like so:
float num = 179.99f;
num.ToString("#.00# 'dollars'");
// Result: 179.99 dollars
Code language: C# (cs)You can also do it without quotation marks as long as they are not reserved characters. If you want to literally display a reserved character, put a backslash in front of it.
float num = 70f;
num.ToString("Number #");
// Result: Number 70
num.ToString("\##");
// Result: #70
num.ToString("\##.00");
// Result: #70.00
Code language: C# (cs)Backslash will escape whatever character that comes after it.
Another way to achieve this is to concatenate strings together like this:
float num = 123.456f;
string message = "The value of num is " + num.ToString();
// Result: The value of num is 123.456
Code language: C# (cs)This is probably the simplest way if you don’t need to format the number.
Conclusion
In conclusion, if you just want to display exactly 2 decimal digits, the F2 specifier is your best and quickest way to achieve that. And if you want more control over how the value looks, there are the “#” specifier and the “0” specifier for you to fine-tune the format.
There are other specifiers for other things like currency or exponential values. I recommend you visit the official .NET’s format types documentation to learn more.
That is all from me. I hope this article is useful to you. Cheers!





