- Math.round in asp.net return 76854.05 instead of 76854.06 ?
- how to solve this.
- how to get 76854.06 value using math.round function???
Asked
Active
Viewed 88 times
-1

user3769935
- 11
- 2
-
10What is the number you are rounding? – faester Jul 29 '14 at 08:05
-
1493501.1*5/100*100/100 – user3769935 Jul 29 '14 at 08:08
-
7Have you looked at the *Note to Callers* on the [MSDN page](http://msdn.microsoft.com/en-US/library/75ks3aby)? – Richard Jul 29 '14 at 08:08
-
1please don't write full question in title. – Amit Kumar Jul 29 '14 at 08:17
-
[Here is an Ideone version](http://ideone.com/oCfrNZ). – Uwe Keim Jul 29 '14 at 08:24
-
Maybe this solves it http://stackoverflow.com/questions/921180/how-can-i-ensure-that-a-division-of-integers-is-always-rounded-up/926806#926806 – Patrick Jul 29 '14 at 08:31
1 Answers
2
Double type should be used just for scientific calculation, for currencies, and types where rounding is important, use decimal instead:
var foobar = Math.Round(1493501.1m * 5 / 100 * 100 / 100, 2);
m suffix stands for decimal type.

Ondrej Svejdar
- 21,349
- 5
- 54
- 89
-
In particular midpoint rules make little sense when rouding `double` numbers to 1 or more decimals. Actually, both `Math.Round(74675.055, 2, MidpointRounding.AwayFromZero)` and `Math.Round(74675.055, 2, MidpointRounding.ToEven)` round ***down*** to a number that looks like `74675.05`. The "midpoint" is not really realizable. Use `decimal`. With `decimal`, both `Math.Round(74675.055m, 2, MidpointRounding.AwayFromZero)` and `Math.Round(74675.055m, 2, MidpointRounding.ToEven)` round ***up*** to `74675.06m` which is an exact number. – Jeppe Stig Nielsen Jul 29 '14 at 08:53