• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Peculiar Problem with Rounding Number ??

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Lot of thanks for all of your's suggestion and code.Now I have sloved my problem.
Thanks & Regards
Bikash
reply
    Bookmark Topic Watch Topic
  • New Topic