question

AsifBhat-3724 avatar image
0 Votes"
AsifBhat-3724 asked Castorix31 answered

Rounding decimals to midpoint/up/down.

Hi

I want to round the decimals like :

2.1 to 2.0
2.2 to 2.0
2.3 to 2.5
2.4 to 2.5
2.5 to 2.5
2.6 to 2.5
2.7 to 2.5
2.8 to 3.0
2.9 to 3.0

How do we do that in C#?



dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
1 Vote"
Castorix31 answered AsifBhat-3724 edited

You can do =>

 decimal nValue = 2.2m;
 Console.WriteLine("{0} --> {1}", nValue, Math.Round(nValue * 2, MidpointRounding.AwayFromZero) / 2);



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.