Given a number how can I round it to some number with two padded zeros

If I have a number 2495, then this should be rounded to 2400

If I have 6321 , then 6300
 123456 , would be 123400
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Relying on the behavior of integer division, you could do:

int number = 6321;
int rounded = (number / 100) * 100;

Open in new window

You need to subtract off the remainder that is left over when you divide by 100.  To do this use the modulo operator.

double abc_1   = 2495;
double abc_1_m = abc_1 - abc_1 % 100;

double abc_2   = 6321;
double abc_2_m = abc_2 - abc_2 % 100;

double abc_3   = 123456;
double abc_3_m = abc_3 - abc_3 % 100;

Open in new window

If you wanted 123456 to be rounded to 123000 then you would subtract (123456 modulo 1000) from 123456 instead of subtracting (123456 modulo 100) from 123456:

double abc_3   = 123456;
double abc_3_m = abc_3 - abc_3 % 1000;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Both Kaufmed and e_livesay, have simple and effective solutions that work equally well.

As e_livesay alluded to, if your stipulation changes (where you have to worry about 10's or 1000's), then you have to make sure to change the equation.

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

käµfm³d 👽Commented:

You need to subtract off the remainder that is left over when you divide by 100.
That's exactly what integer division does  ; )

Agreed - the solutions are equivalent.  Didn't see your response until I'd written mine.
käµfm³d 👽Commented:

All good. I mistook the "... when you divide by 100" to be pseudo-directed at me  = )

No problem.  Wondered if it might look like that after I saw that your post had come in before mine.  Not directed at you at all.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.