Hi,
I have made an application for rounding off any number to nearest tens,hundreds or thousands....
But for some values my code is giving wrong answer...
Like , if i want to solve "25*89" ,in that rounding off 25 to nearest tens , the answer should be 30 (according to books) , but my code is giving answer as 20...........

``````private void button2_Click(object sender, EventArgs e)
{
a = this.textBox1.Text;
b = this.textBox2.Text;
c = this.textBox3.Text;
i = Convert.ToInt32(a);

j= Convert.ToInt32(b);

k= Convert.ToDouble(c);
d1 =Math.Round(i / k) * k;
d2 =Math.Round(j / k) * k;
}``````

How can i do this?

Post your code with hardcoded values so I know what the input is.

Post your code with hardcoded values so I know what the input is.

my file is attached here........
for rounding off 25 to nearest 10,its giving 20 ,but it should give 30................

You need to make your own rounding function:

``````private static int Round(int Value, int Places)
{
//25 / 10 = 2.5
if ((Places % 10) != 0)
throw new ArgumentException("Must be a power of 10", "Places");
decimal dFractal = Convert.ToDecimal(Value) / Convert.ToDecimal(Places); //2.5
int dResult = Convert.ToInt32(Math.Truncate(dFractal) * Places); //20
if ((dFractal - Math.Truncate(dFractal)) >= 0.5M)
dResult += Places;
return dResult;
}``````

Results in:

``````Rounding 24 equals 20
Rounding 25 equals 30
Rounding 26 equals 30
Rounding 263 equals 260
Rounding 264 equals 260
Rounding 265 equals 270
Rounding 266 equals 270``````

Test with:

``````private void button1_Click(object sender, EventArgs e)
{
int i = 24;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 10));
i = 25;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 10));
i = 26;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 10));
i = 263;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 10));
i = 264;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 10));
i = 265;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 10));
i = 266;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 10));
}``````

Actually, the last one had a little bug:

``````private static int Round(int Value, int Places)
{
//25 / 10 = 2.5
if (Places < 10)
throw new ArgumentOutOfRangeException("Places", "Must be greater than or equal to ten");
if ((Places % 10) != 0)
throw new ArgumentException("Must be a power of 10", "Places");
decimal dFractal = Convert.ToDecimal(Value) / Convert.ToDecimal(Places); //2.5
int dResult = Convert.ToInt32(Math.Truncate(dFractal) * Places); //20

if (Math.Abs((dFractal - Math.Truncate(dFractal))) >= 0.5M)
dResult += (Places * (Value > 0 ? 1 : -1));

return dResult;
}``````

But this is for tens............do i have to make separate functions for hundreds and thousands........

No.

``````private void button1_Click(object sender, EventArgs e)
{
int i;
i = 249;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 100));
i = 250;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 100));
i = 251;
Console.WriteLine("Rounding {0:F0} equals {1:F0}", i, Round(i, 100));
}``````

Results in:

``````Rounding 249 equals 200
Rounding 250 equals 300
Rounding 251 equals 300``````

Just change the second parameter to 10,100,1000

Get rid of the habbit of naming your variables a, b, c etc.
Give meaningfull names to them. When you come back later to your code and you read something like MyNumberOfCars, it says more than just c . Now you have to wonder, "What was that c again?"
Now what you want is a biased round and that's not good!
The Math Round function does it's work perfectly, try 35 it should give 40!
In your case you always want to round up. If you want a mean from your data that means that your mean will be higher then must be.
edit : Guess I missed a few posts here. I guess you could do what Scott suggests : write your own Round function.

commented: so very very true +7

but this is not running for my window application which i sent to u