toto - 8 months ago 83

ASP.NET (C#) Question

I'm struggling with a stupid issue !!

I get a number from a calcul and I get : 1.9976597777777

And I want to Round it to 2.00

So I use Math.Round(1.9976597777777) and I get 2

But when i do this round with another number like Math.Round(1.5) I get 1 but I need 1.50

I'm sure it's easy but I don't understand the logic.

P.S : In JavaScript globalize format number works fine...

Answer

Have you tried reading the documentation?

Rounds a double-precision floating-point value to the nearest integral value.

It's working as expected. If you want 2 dp, you have to specify that using another overload:

```
Math.Round(1.9976597777777, 2) // 2.00
```

Source (Stackoverflow)