Solved

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

Posted on 2014-11-13
7
82 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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@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
Comment Utility
@kaufmed

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

Expert Comment

by:käµfm³d 👽
Comment Utility
@e_livesay

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

Expert Comment

by:e_livesay
Comment Utility
@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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

10 Experts available now in Live!

Get 1:1 Help Now