well i have very big problem in Round Function when i m putting 3 digit number in text box than its not give me round function but if i m putting 2 digit number in text box than its convert into round function i dont know what wrong with that..............
double num = 100.5; num = Math.Round(num);//100
double num1 = 100.51; num = Math.Round(num);//101
but i want 100.5 as 101..........
hope u understand my problem.
thanks in Advance
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Nilanka DharmadasaPosted Dec 23, 2009, 5:12 AM
Math.Round rounds the number based on the normal rounding concept.
It means
100.48 >> 100
100.5 >> 100
100.51 >> 101
100.6 >> 101
If you want to get 101 for all , then you can use Ceiling.
100.48 >> 101
100.5 >> 101
100.51 >> 101
100.6 >> 101
double num = 100.5;
num = Math.Ceiling(num);//101
If you want to get 101 only for 100.5 and all values greater than that you have to write your own method like below.
public double MyRound(double input)
{
if ((input - Math.Floor(input)) == 0.5)
return Math.Ceiling(input);
else
return Math.Round(input);
}
private void button1_Click(object sender, EventArgs e)
{
double num = 100.5;
num = MyRound(num);//101
}
srinivas reddyPosted Dec 23, 2009, 6:59 AM
you can use MidpointRounding mode,and use AwayFromZero mode...so that u can get your desired output.
Math.Round(100.5,MidpointRounding.AwayFromZero
it gives 101.