IntelliJ Java IDE
The moose likes HTML, CSS and JavaScript and the fly likes Peculiar Problem with Rounding Number ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Peculiar Problem with Rounding Number ??" Watch "Peculiar Problem with Rounding Number ??" New topic
Author

Peculiar Problem with Rounding Number ??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 340
Hi Eric,
Iam facing a peculiar problem with rounding number,iam using below function for rounding number and both parameter of my function is coming from database depends on some conditions.Now my problem is my function is working fine as per my requirement accept the number which is above 5000000 and only in case of rounding position after decimal and that value of number is greater than and equal five.
For example :-
Suppose number is 50000000.61994824 and round place is -2 then my function rounded the number to 50000000.62 and it is working fine with any value of round place.
But if the number is 51000000.61994824 and round place is -2 then my function rounded the number to 51000000.620000005 which is wrong rounded number, rounded number should be 51000000.62.I have tested with different negative value of round place if that value of number is greater than and equal five then only it is giving me problem but if that value of number is smaller than five then it is working fine.And my function also working fine with all positive value of round place with any number.
Can you please test my function with my above given number and please guide me how i can slove this problem.

Thanks & Regards
Bikash
[ March 17, 2004: Message edited by: Bikash Paul ]
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 411
Hi,
I changed your function to not use negative pow, it it seems to work fine:
function RoundNumber(theNum)
{
var a = Math.pow(10,2);
var b = theNum * a;
var c = Math.round(b);
return c/a;
}
Also, for IE you could use Number.toFixed:
<input type=text name="t2" value="" onBlur="if(!isNaN(this.value)){this.value=(new Number(this.value)).toFixed(2);}">
But I am not sure is this function supported by other browsers
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 340
Hi,
I think you are not getting my point my function working fine accept above defined two situation and my round place may be any negative or positive value that i don't know cause my number and round place will come from data base in my jsp page depends on some condition dynamically.I can't fixed my round place thats why my function must have two parameters one is that number which my function have to round and second is round place.Here I have given fnTest() function in my above code only for testing my RoundNumber function,iam not using fnTest() function in my actual code,I am using only RoundNumber function.And in my actual code another function calling my RoundNumber function and passing those parameter(theNum and roundPlace)dynamically.Now i think you are clear about my problem.
Eric can you please help me to slove this problem.
Thanks & Regards
Bikash
[ March 17, 2004: Message edited by: Bikash Paul ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
Sounds like you found one of the bugs with numbers with computing.
The only thing I can suggest is you can check for the number of decimal places there are, if it id greater then 4 then chop off the rest...
It is a pain for just a couple of exceptions.
Eric
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
To expand on Eric's reply, computers cannot represent some fractions exactly using base 2. We have the same problem with numbers in base 10. What is the base 10 representation of 1/3? It's 1.3333(3) - a repeating decimal fraction.
The same thing happens with base 2 numbers. 1/5 which we can easily represnet in base 10 as 0.2, cannot be accuratly represented in base 2. The base 2 representation is 0.00110011(0011). The 0011 keeps repeating. What happens in the computer is the repeating decimal fraction is truncated when it reaches the number of bits used to represent the number. This value is then rounded for display resulting in the "5" digit way out at the end of your number.
Eric presented the correct solution. Not only do you need to round your number, you also need to limit the number of characters displayed.
Hope this helps,
Tom Blough


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 411
Well, if you what me to spell it out, here it is:
Out of three round functions in the following example, yours is the only one having problem you described.
<SCRIPT>
function RoundNumber1(theNum,roundPlace)
{
var realRoundPlace = roundPlace * -1;
var a = Math.pow(10,realRoundPlace);
var b = theNum * a;
var c = Math.round(b);
return c/a;
}
function RoundNumber2(theNum,roundPlace)
{
//this function may be IE only
var realRoundPlace = roundPlace * -1;
return new Number(theNum).toFixed(realRoundPlace);
}
//this one is yours
function RoundNumber(theNum,roundPlace) {
multiAmt = Math.pow(10,roundPlace)
theAns = Math.round(theNum/multiAmt)*multiAmt;
return theAns;
}
</SCRIPT>
<FORM>
<INPUT NAME=s1 onchange="this.value=RoundNumber1(this.value,-2)">
<INPUT NAME=s2 onchange="this.value=RoundNumber1(this.value,-4)">
<INPUT NAME=s3 onchange="this.value=RoundNumber2(this.value,-2)">
<INPUT NAME=s4 onchange="this.value=RoundNumber2(this.value,-4)">
<INPUT NAME=s5 onchange="this.value=RoundNumber(this.value,-2)">
<INPUT NAME=s6 onchange="this.value=RoundNumber(this.value,-4)">
</FORM>
If you whant to use those functions in your code, you will need to remove the number at the end of function name.
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 340
Hi all,
Lot of thanks for all of your's suggestion and code.Now I have sloved my problem.
Thanks & Regards
Bikash
 
 
subject: Peculiar Problem with Rounding Number ??
 
Threads others viewed
simple problem,pl. help
Rounding Input Value ??
how to set value of text field
IP address validation using javascript
Any other way for Customize confirmation alert Msg Box??(Mr. Al Ian)
developer file tools