.NET rounding error in ToString ("f2")

Hi, I have this code in C #:

float n = 2.99499989f;
MessageBox.Show("n = " + n.ToString("f2", CultureInfo.InvariantCulture));

And this code in C ++:

float n = 2.99499989f;
printf("n = %.2f", n);

The first prints 3.00 .
The second prints 2.99 .

I do not know why this is happening.

Update:

I also tried Objective-C NSLog , and the result is 2.99 .

I needed to fix it quickly, so I used the following method:

float n = 2.99499989f;
float round = (float)Math.Round(n, 2);
MessageBox.Show("round = " + round.ToString(CultureInfo.InvariantCulture));

This code shows 2.99 , but calculates the circle in double precision. I can not find Math.RoundF.

+5
source share
6 answers

( , , , ...)

, "printf" float double . float double, ToString ( "f2" ), float.

printf:

C float double ( , , , int printf...

fooobar.com/questions/369917/...

fooobar.com/questions/146402/...

, , 2.995F = 2.99499989F, , 2.995 .

, , , , 2.995 < > 2.99499989 , 2.995 ( ), , .

, 2.99499989F 3.00, , -, 2.995, , 2.99499989 , , "" , base-10 base-2, 1 0, 10, , . , , , 2 base-10, , float, - 2.995.

+1

BitConverter.GetBytes , , - 0x403FAE14,

2.99499988555908203125

, printf ToString. , .

+6

.

-, .

, ? 2.9949... 2.995?

0

, "float" ""?

, :

        // float
        Console.WriteLine (2.99499989f.ToString ("f2"));
        // The above line outputs 3.00

        // decimal
        Console.WriteLine (2.99499989M.ToString ("f2"));
        // The above line outputs 2.99

, float 2.99499989 2,99.
, , :

// float
Console.WriteLine (2.98499f.ToString ("f2"));
// The above line outputs 2.99
0

2^{-23} 0,0000001192 ( ). , 2.995-2.99499989 = 0,00000011. 3,6*10^{-8}.

- 2.99499989f, 2.995. 2.994999 ( 2 ^ {- 23}, ), , 2.995

0

2.99499989f IEEE754. , printf ToString -.

-1
source

All Articles