Solved

Rounding a decimal 5.125 to 5.13

Posted on 2004-08-07
11
1,558 Views
Last Modified: 2012-08-13
How do I round a variable that equals 5.125 or something at the 5 range to always round up - in this case to 5.13.  Right now, I'm doing Round(x,2) with x = 5.125 and it still outputs 5.125.

Thanks.
0
Comment
Question by:tqwu
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11744905
Hi

It starts counting at 0. Use Round(x,1)

Regards,
Zyloch
0
 
LVL 22

Expert Comment

by:cookre
ID: 11744924
Before library functions came along to do it, we would round to the i-th place to the right by adding  5*10**(-i)
0
 

Author Comment

by:tqwu
ID: 11744952
Using Round(x,1) gives me 5.1
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11744954
Hmm... Yea, it should be Round(x,2) [Sorry for my temporarily lapse of thought].

That's odd. Try changing x to 5.1255 and see what happens.
0
 
LVL 22

Expert Comment

by:cookre
ID: 11744976
Alas, the Round() method doesn't always round up.

From the c# help:
"When d is exactly halfway between two rounded values, the result is the rounded value that has an even digit in the far right decimal position. For example, when rounded to two decimals, the value 2.345 becomes 2.34 and the value 2.355 becomes 2.36. This process is known as rounding toward even, or rounding to nearest."

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:tqwu
ID: 11744986
....and how to do solve this problem...?
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11744990
Interesting cookre. I read about that once in a math book. It's something called significant digit rounding or something that's supposed to minimize error in large scale rounding. I dunno how that really works to minimize those errors, but they say it does.

tqwu: Have something like this instead of round:

ceil(x*100)/100
0
 
LVL 22

Expert Comment

by:cookre
ID: 11745009
How is x typed?
0
 
LVL 3

Accepted Solution

by:
travisjhall earned 500 total points
ID: 11745015
That's interesting. I'm seeing strange results from a similar operation here. Any time the digit in the decimal place to be rounded to is even, the following digit is 5, and no other digits follow, it rounds down. I would guess that this has to do with a slight inaccuracy inherent in the representation of the numbers involved in binary. (Because we can't use an infinite number of binary digits, some numbers can't be represented precisely.)

It took me too attempts to find a way around this. My first correction seemed to run straight into the original problem. However, this function seems to do the trick:

Function TrueRound(number, places)
  TrueRound = Round(number, places)
  If number * 10^places - TrueRound * 10^places = 0.5 Then TrueRound = TrueRound + 10^(-places)
End Function

Use TrueRound wherever you would otherwise be using Round, and I think you'll be okay.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11745029
Is this in ASP or PHP? Anyways, why not just have something like this?

Function TrueRound(number,places)
   TrueRound = Ceil(number*(Math.pow(10,places)))/Math.pow(10,places)
End Function

(If the above funciton is wrong, sorry, I'm not too good with ASP, but the concept remains, this is it in PHP:

function TrueRound($number,$places) {
   return(ceil($number*(pow(10,$places)))/pow(10,$places));
}


   
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11745035
Ah, a slight mistake. In my case, it'll always round up no matter even if it's 5.124 or whatever. travis's should work
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
squareUp  challenge 22 106
array6 challenfge 6 63
pairs challenge 5 44
index Out OF Range Exception error 4 29
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
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 …

706 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

16 Experts available now in Live!

Get 1:1 Help Now