Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how do I find the closest number divisible by 10

Posted on 2013-01-27
8
Medium Priority
?
549 Views
Last Modified: 2013-01-31
I need a function where it will give me the closest number divisible by 10.  The input is a decimal so 85.92 is valid and the output would be 90.

so if the input was
81.9 = 80
84.34 = 80
89.11 = 90
91.4 = 90
85.0 = 90 (this wouldn't happen much because the input is a decimal)
0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38825116
Try:
y = 10 * Math.Round(x / 10);

Open in new window

0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 1000 total points
ID: 38825119
A bit more elaborate (and taking types in account):
private void Form1_Load(object sender, EventArgs e) {
    MessageBox.Show(findClosestDivisible((decimal)84.9, 10).ToString());
}

private int findClosestDivisible(decimal x, decimal divisor) {
    return (int)(divisor * Math.Round(x / divisor, MidpointRounding.AwayFromZero));
}

Open in new window

0
 
LVL 10

Expert Comment

by:Monica P
ID: 38825696
Try this logic in your code

Divide by 10, then truncate, then multiply by 10.

229 / 10 = 22,90000...
Math.Truncate(22,9) = 22
22 * 10 = 220.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38825853
@AkilaPalanimuthu: shouldn't 229 be rounded up to 230?
0
 
LVL 10

Expert Comment

by:Monica P
ID: 38825877
229 is just input number..

Incase of 229.5 it can be rounded to 230 and then can apply the logic
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38825883
That's not what I meant. Maybe I read the question wrong but 229 is closer to 230 than to 220.
0
 
LVL 10

Assisted Solution

by:John Claes
John Claes earned 1000 total points
ID: 38825951
robert_schutt ,  AkilaPalanimuthu:

Combine your comments ;-)

229 / 10 = 22,90000...
Math.Round(22,9) = 23
23 * 10 = 230.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38825976
That was actually my first comment but then I found that code-wise it needed some enhancements, hence my second comment. Also just for fun adding the possibility to use another number than 10. The Math.Round documentation explains that by default 4.5 is rounded down to 4, so even though the OP specified it would be rare, I included the 'MidpointRounding.AwayFromZero' argument for what I shamelessly assume to be mathematically 'normal' behaviour.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 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