decimial operations in javascript. best way to do a simple substraction ?

Discussion in 'Javascript' started by sonic, May 2, 2005.

  1. sonic

    sonic Guest

    Hello,
    What is the correct way of performing this substraction in JS.

    var a = '29.33'; ( or 29.33 )
    var b = '0.01'; ( or 0.01 )

    var result = a - b;

    ( result = 29.319999999999996; )

    my result is rounded off in the way floats would round off :/
     
    sonic, May 2, 2005
    #1
    1. Advertising

  2. sonic wrote:
    > What is the correct way of performing this
    > substraction in JS.


    Using the subtraction operator.

    > var a = '29.33'; ( or 29.33 )
    > var b = '0.01'; ( or 0.01 )
    >
    > var result = a - b;


    Yes, that is the one.

    > ( result = 29.319999999999996; )
    >
    > my result is rounded off in the way floats would
    > round off :/


    Javascript's one number type is a 64 bit IEEE double precision floating
    point number.

    Richard.
     
    Richard Cornford, May 2, 2005
    #2
    1. Advertising

  3. sonic

    SoniC SouL Guest

    thats great. i just dont want the crazy rouding to appear. what do you
    suggest i do to show the result as currency ?

    *** Sent via Developersdex http://www.developersdex.com ***
     
    SoniC SouL, May 2, 2005
    #3
  4. JRS: In article <>
    , dated Mon, 2 May 2005 09:59:24, seen in news:comp.lang.javascript,
    sonic <> posted :

    >What is the correct way of performing this substraction in JS.
    >
    >var a = '29.33'; ( or 29.33 )
    >var b = '0.01'; ( or 0.01 )
    >
    >var result = a - b;
    >
    >( result = 29.319999999999996; )
    >
    >my result is rounded off in the way floats would round off :/


    If those are Euros, and the inputs are meant to be exactly what you
    wrote, then do the calculation in cents; or use StrU etc.

    Read the newsgroup FAQ.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, May 2, 2005
    #4
  5. Re: decimial operations in javascript. best way to do a simple substraction?

    On 02/05/2005 19:12, SoniC SouL wrote:

    > thats great. i just dont want the crazy rouding to appear. what do you
    > suggest i do to show the result as currency ?


    Perform the operations in pennies/cents/whatever so that you only use
    integers. You can then use the function below to create a string
    representing the value in pounds/dollars/whatever.

    /* n - Number to format (in pennies).
    * c - Currency symbol to use (defaults to none).
    * t - Thousands separator (defaults to none).
    * d - Decimal separator (defaults to '.').
    *
    * Outputs a number of the form cntnnntnnndnn
    *
    * For example, toCurrency(142635.7, '£', ',') produces
    * £1,426.36
    */
    function toCurrency(n, c, t, d) {
    var s = (0 > n) ? '-' : ''; n = Math.abs(n);
    var m = String(Math.round(n));
    var j, i = '', f; c = c || ''; t = t || ''; d = d || '.';

    while(m.length < 3) {m = '0' + m;}
    f = m.substring((j = m.length - 2));
    while(j > 3) {
    i = t + m.substring(j - 3, j) + i;
    j -= 3;
    }
    i = m.substring(0, j) + i;
    return s + c + i + d + f;
    }

    Notice the rounding of the floating-point value in the example.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, May 2, 2005
    #5
  6. Re: decimial operations in javascript. best way to do a simple substraction?

    sonic wrote:
    > What is the correct way of performing this substraction in JS.
    >
    > var a = '29.33'; ( or 29.33 )
    > var b = '0.01'; ( or 0.01 )
    >
    > var result = a - b;
    >
    > ( result = 29.319999999999996; )
    >
    > my result is rounded off in the way floats would round off :/


    This is an inherent problem with floating-point arithmetic.
    Floating-point should never be used when decimal precision is required,
    such as in applications concerning money.

    Use scaled integers instead. Instead of working in dollars, work in
    cents. Integer arithmetic is precise.

    http://www.crockford.com/javascript
     
    Douglas Crockford, May 2, 2005
    #6
  7. sonic

    Peter Wilson Guest

    Re: decimial operations in javascript. best way to do a simple substraction?

    sonic wrote:
    > Hello,
    > What is the correct way of performing this substraction in JS.
    >
    > var a = '29.33'; ( or 29.33 )
    > var b = '0.01'; ( or 0.01 )
    >
    > var result = a - b;
    >
    > ( result = 29.319999999999996; )
    >
    > my result is rounded off in the way floats would round off :/
    >

    var result = (a-b).toFixed(2)

    Pete
    --
    Peter Wilson
    http://www.whitebeam.org
    ----


    --
    Peter Wilson
    T: 01707 891840
    M: 07796 656566
    http://www.yellowhawk.co.uk
    <http://www.yellowhawk.co.uk>

    ------------------------------------------------------------------------
     
    Peter Wilson, May 3, 2005
    #7
  8. sonic

    Randy Webb Guest

    Re: decimial operations in javascript. best way to do a simple substraction?

    Peter Wilson wrote:

    > sonic wrote:
    >
    >> Hello,
    >> What is the correct way of performing this substraction in JS.
    >>
    >> var a = '29.33'; ( or 29.33 )
    >> var b = '0.01'; ( or 0.01 )
    >>
    >> var result = a - b;
    >>
    >> ( result = 29.319999999999996; )
    >>
    >> my result is rounded off in the way floats would round off :/
    >>

    > var result = (a-b).toFixed(2)


    And then wonder what went wrong when tested in IE?

    Read the group FAQ, explicitly section 4.6 with regards to that.

    --
    Randy
     
    Randy Webb, May 4, 2005
    #8
  9. SoniC SouL wrote:

    > thats great. i just dont want the crazy rouding to appear. what do you
    > suggest i do to show the result as currency ?


    I suggest you read the FAQ and improve your posting style.


    PointedEars
     
    Thomas 'PointedEars' Lahn, May 13, 2005
    #9
  10. JRS: In article <>, dated Fri, 13 May
    2005 21:08:39, seen in news:comp.lang.javascript, Thomas 'PointedEars'
    Lahn <> posted :
    >SoniC SouL wrote:
    >
    >> thats great. i just dont want the crazy rouding to appear. what do you
    >> suggest i do to show the result as currency ?

    >
    >I suggest you read the FAQ and improve your posting style.


    Citing a document and not indicating its location is unintelligent.

    So is reviving a thread that came to a satisfactory conclusion a week
    ago. You do not seem to realise that a future potential employer may do
    an Internet search in order to investigate not only your technical
    knowledge but also your personality and judgement.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, May 14, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,283
    robert
    Feb 11, 2006
  2. Replies:
    2
    Views:
    332
    Johnny Google
    Nov 2, 2004
  3. Replies:
    4
    Views:
    347
  4. Josh Mcfarlane
    Replies:
    4
    Views:
    307
    Axter
    Dec 7, 2005
  5. Jenny

    a program of substraction

    Jenny, Nov 1, 2008, in forum: C Programming
    Replies:
    1
    Views:
    263
    Jenny
    Nov 1, 2008
Loading...

Share This Page