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 135 times
-1

FernandoPaiva
- 4,410
- 13
- 59
- 118
-
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;

Chris Mack
- 5,148
- 2
- 12
- 29
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

Taro
- 126
- 9