Solved

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

Posted on 2014-11-13
7
85 Views
Last Modified: 2015-01-08
If I have a number 2495, then this should be rounded to 2400

If I have 6321 , then 6300
 123456 , would be 123400
0
Comment
Question by:countrymeister
  • 3
  • 3
7 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 40440281
Relying on the behavior of integer division, you could do:

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

Open in new window

0
 

Accepted Solution

by:
e_livesay earned 250 total points
ID: 40440334
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

0
 
LVL 33

Expert Comment

by:it_saige
ID: 40440336
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.

-saige-
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40440640
@e_livesay

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

Expert Comment

by:e_livesay
ID: 40440888
@kaufmed

Agreed - the solutions are equivalent.  Didn't see your response until I'd written mine.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40440914
@e_livesay

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

Expert Comment

by:e_livesay
ID: 40440934
@kaufmed.

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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in page 3 46
How to avoid saving tif files a 2nd time using VS2015 and C#? 1 27
Capture logon name 13 31
Coding C# in Linux 8 24
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now