PDA

View Full Version : math problem?! error adding values

a|landargin
02-25-2003, 05:14 PM
hi!
i'm running a webshop where in the end all values from chosen articles are added, and a end-value is shown.

the problem is now that the script makes strange errors adding those values. there's not always this error, but sometimes.

for example it should ad 13 positions and get 140.25

but it displays 140.24999999999997 !

if it should get 135.14, f.e. it makes it alright and gets 135.14

i'm confused.. this is just simple adding, why these errors?

beetle
02-25-2003, 05:37 PM
I read about the source of these little rounding errors once, but I've forgotten why it happens. It just does. Applying a rounding method and you'll be fine.

Number.prototype.round = function( places )
{
var factor = parseInt( "1E" + places, 10 );
return Math.round( this * factor ) / factor;
}

var num = 140.24999999999997;

a|landargin
02-26-2003, 11:07 AM
this does not help me :(

mordred
02-26-2003, 11:42 AM
And your problem is exactly... what?

Seriously, "this does not help me" is not an adequate description of your problem. You just make it harder for those who want to help, because they have test the code themselves and that might lead to totally different results depending on their particular environment.

Anyway, FWIW, you might try changing parseInt to parseFloat, because beetle tries to parse a number in scientific notation. You won't come around rounding values because computers store numbers in a binary representation, and some fractions are not exactly transferable to binary, that's how it is.

Stoffel
02-26-2003, 11:49 AM

a|landargin
02-26-2003, 01:03 PM
var gtotal=6.72;
function getBetr(b)
{
k = (Math.round(100 * (b + 0.00001)) / 100 + 0.001) + '';
k = k.substring(0, k.indexOf('.') + 3);
return k;
}

...

document.writeln('<input type="hidden" name="aaa" value="'+gtotal+'">');
}

...

document.write(getBetr(gtotal));

i did it that way but it wont work.. sorry i'm bugging you guys.

mordred
02-26-2003, 01:20 PM
Works for me, your error might be somewhere else. Since you refuse to provide a detailed problem description, it's really hard to guess a solution.

a|landargin
02-26-2003, 01:33 PM
i don't refuse nothing. i'm just doinng my best to explain the problem but i guess nobody wants me to post 2 pages of code here.

mordred
02-26-2003, 02:18 PM
But you don't really explain your problem... you just say it doesn't work, but doesn't give us an idea what did not work...

Look here why I said that the code worked for me (IE5.5 on WinNT):

var gtotal=6.72;

function getBetr(b)
{
k = (Math.round(100 * (b + 0.00001)) / 100 + 0.001) + '';
k = k.substring(0, k.indexOf('.') + 3);
return k;
}

If you just say "it does not work", that is not very telling... does any JavaScript error pop up? If so, what is the error message? Does the browser crash (unlikely, but happens from time to time)? Or is the result not what you expected (like rounding down to three fractions)? If so, what result did you expect? And finally, what browser on what platform do you have that problem (whatever it may be)?

a|landargin
02-26-2003, 02:34 PM
what i ment was that i did't get get desired results, the error is permanent.

(140.24999999999997 instead of 140.25 , f.e. )

i use ie 6.02 on win2k.

there is no error popping up or anything. the problem is that the script is very comlplex and the value is changed several times. by now i could not figure ou where exactly the problem is. i was just asking for hints what it COULD be..

beetle
02-26-2003, 03:18 PM
getBetr?? What does that mean? "get better"??

Just apply the rounding methos I wrote...it's easy! The single argument is for the number of places left of the decimal to round to. You should always round your result, whether it needs rounding or not!

Do you not understand how to use the method from the example I gave?

a|landargin
02-26-2003, 03:27 PM
but i did use a rounding as you can see.. it just wont work.
never mind this getbetr :)

mordred
02-26-2003, 03:29 PM
I think that "Betr" stands for "Betrag", which roughly translates to value/amount.

Anyway, a|landargin, if your end result displays a non-rounded value, then there is one rounding too few in your code. Why not apply the rounding function exactly at the end, before the value is written to the hidden field?
Another reason could be that you call the function correctly, but don't assign the return value to your gtotal variable.

a|landargin
02-26-2003, 03:49 PM
thank you.. i'll check that