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

Recommended Answers

All 6 Replies

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

Thanks LizR

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.