- Home /
Double Round Problems
I want to round a number to only 2 decimals and Math.Round works just fine until the number is 1e+15 or greater and unity starts showing it with e instead and I get 14 decimals.
Math.Round(1.12345, 2) = 1.12
Math.Round(1.12345E+15, 2) = 1.12345E+15
How do I make it show only 2 decimals when unity shifts to using e?
Comment
Answer by tanoshimi · Apr 27, 2015 at 07:41 PM
Math.Round rounds a value to the specified number of fractional digits. But 1.12345E+15 doesn't have any fractional part. It's the number 1,123,450,000,000,000. Which, when rounded to two fractional digits, is still 1,123,450,000,000,000.
Secondly, why do you want to do this? It sounds like it might be for display purposes, in which case you shouldn't be using Math.Round anyway. You should use, say, .ToString("N2");
instead.