Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
HI friends

I have a number like 235. i want to round this number to 300. Like 85672 - 90000

how can i achieve this

I used Math.cieling(number/100.000d)*1000
but if a no is 312 it wil remain as 312 not 400

Pls help

Regards
Chinnu
Posted
Comments
Dr.Walt Fair, PE 17-Jul-11 1:43am    
Aside from what I assume are typos, that function should work. What language are you using and exactly what lines of code?

I just tried it (because it looks fine to me, excepting the multiply is out by an order of magnitude:
int i = 312;
Console.WriteLine(Math.Ceiling(i / 100.0d) * 100);
The result I got was "400".
What are you doing differently?
 
Share this answer
 
Here's a simple solution:
C#
int x = 235;
int remainder = x % 100;
if (remainder > 0) {
    x = x + (100 - remainder);
}
 
Share this answer
 
v2
Comments
Philippe Mori 17-Jul-11 18:53pm    
or simply x = ((x + (100 - 1)) / 100) * 100 if x is an integer type.
[no name] 17-Jul-11 23:53pm    
Nice, but my solution is simple and is better in time complexity and space complexity.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900