Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Feb 2003
    Location
    munich, germany, old europe
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    math problem?! error adding values

    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?
    Last edited by a|landargin; 02-25-2003 at 07:22 PM.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    Number.prototype.round = function( places )
    {
        var factor = parseInt( "1E" + places, 10 );
        return Math.round( this * factor ) / factor;
    }
    
    var num = 140.24999999999997;
    alert( num.round( 2 ) );
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    New Coder
    Join Date
    Feb 2003
    Location
    munich, germany, old europe
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this does not help me

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New Coder
    Join Date
    Jan 2003
    Location
    Belgium
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Take a look at this. I had the same problem
    SToFFeL

  • #6
    New Coder
    Join Date
    Feb 2003
    Location
    munich, germany, old europe
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    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.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    New Coder
    Join Date
    Feb 2003
    Location
    munich, germany, old europe
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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):

    Code:
    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;
    }
    
    alert(gtotal + 0.1);			// <- alerts '6.819999999999', which is not wanted
    alert(getBetr(gtotal + 0.1)); 	// <- alerts '6.82', which is good
    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)?

  • #10
    New Coder
    Join Date
    Feb 2003
    Location
    munich, germany, old europe
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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..

  • #11
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #12
    New Coder
    Join Date
    Feb 2003
    Location
    munich, germany, old europe
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but i did use a rounding as you can see.. it just wont work.
    never mind this getbetr

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #14
    New Coder
    Join Date
    Feb 2003
    Location
    munich, germany, old europe
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you.. i'll check that


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •