Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

Round up trailing 5 with Math.round

The result of this code,

string leftVal = foo();
Math.Round((decimal)float.Parse(leftVal), 3, MidpointRounding.AwayFromZero);

is 3285.812, but I need 3285.813.

How can I get the desired value?

Upvotes: 1

Views: 70

Answers (1)

Kyle
Kyle

Reputation: 6684

Rather than parsing the string as a float and then casting to a decimal, you should probably just parse it straight to a decimal:

string leftVal = foo();
Math.Round(decimal.Parse(leftVal), 3, MidpointRounding.AwayFromZero);

It seems that the cast to decimal is what's rounding.

Upvotes: 3

Related Questions