Simply
Simply

Reputation: 25

How to show a number with 2 decimal places?

I am aware that this question has been asked on here before, but every single solution I used still doesn't work and the text just says 1.00 from everything from 1.00 to 1.99, so it doesn't show any decimal places. My current code is this:

doubleNum = CountiesList[Current].Infected / 1000000;
    if (eachChild.name == "Text2")
        eachChild.GetComponent<Text>().text = doubleNum.ToString("F", CultureInfo.InvariantCulture) + "m"; 

I have tried lots of solutions that I have found here and on other sites such as:

decimalNum.ToString("0.##") + "m";

(and I used a decimal number here instead of a double number)

Math.Round(doubleNum, 2);
Math.Round(doubleNum, 2, MidpointRounding.AwayFromZero)
Math.Truncate(doubleNum * 100) / 100;
doubleNum.ToString("F")

However, none of these have worked so far as they all display it as 1.00 or 2.00 or whatever without showing any numbers after the decimal place (except 0). Thanks.

Upvotes: 0

Views: 100

Answers (1)

Victor Chelaru
Victor Chelaru

Reputation: 4847

Assuming CountiesList[Current].Infected is an integer, then dividing CountiesList[Current].Infected by 1000000 producers an integer, which is probably either 1 or 2 in your case.

If you want the result to be a double, one of the two values should be a double:

doubleNum = CountiesList[Current].Infected / 1000000d;

More detailed discussion here:

C#: divide an int by 100

Example dotnetfiddle here:

https://dotnetfiddle.net/gT4PKN

Upvotes: 1

Related Questions