I have a decimal number decimal n = 0.111111111m;
and I want to change it to 0.112
. How could I do this ?
Asked
Active
Viewed 78 times
-1
![](../../users/profiles/1483084.webp)
FernandoPaiva
- 4,220
- 11
- 48
- 105
-
This answer shows how to round to 2 decimal places, change the number from 2 to 3:https://stackoverflow.com/questions/257005/how-do-you-round-a-number-to-two-decimal-places-in-c – Sparrow Nov 22 '17 at 23:05
-
What results do you expect for these sample inputs - `0.1111` `0.1115` `0.1125` `-0.1111` `-0.1115` `-0.1125`? – mjwills Nov 23 '17 at 00:41
2 Answers
2
You can use:
Math.Round(n, 3);
To always round up, you can use:
Math.Ceiling(n * 1000) / 1000;
![](../../users/profiles/5314530.webp)
Chris Mack
- 4,928
- 2
- 8
- 22
0
According to this link https://msdn.microsoft.com/en-us/library/6be1edhb(v=vs.110).aspx
You'll need to do something like Decimal.Round(n, 3);
However your initial approximation is not accurate
![](../../users/profiles/7264883.webp)
Taro
- 116
- 8