I want to round number
for example number 2.8 to 3
and 2.4 to 2.5
how I made so in C#
I heard something about Math.Round function but it doesn't work properly

Thanks Sergey

9 Years
Discussion Span
Last Post by ddanbe

That functions for decimals, rather than to integers so.. theres 3 functions (which your helpfile would have mentioned linked to round)

round - rounds 2.454 to 2.54
floor - takes 2.45 to 2, and 2.8 to 2
ceiling - takes 2.45 to 3 and 2.8 to 3

round is the right one but you didnt look at all the options.. theres one which says what to do on a midpoint and beaware, 2.50 would be rounded to 2, where as 2.51 would be rounded to 3


I found a way with :
Math.Round(decimal type variable)
it works perfect with decimal variables do you know the same way what work with double ?


There is as of .NET 2.0 a Math.Truncate function. It migght help.

double num;
num = 2.89;
num = Math.Truncate(num);

I made simple tried and result I get 2 and not 3 like I wanted you gave me a tool to work with double types but it's round it to smallest one.

Thanks anyway Sergey

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.