Format to Currency when multiplying/Summing fields

Discussion in 'Javascript' started by Targa, May 12, 2004.

  1. Targa

    Targa Guest

    <input NAME="TAXRATE" onBlur="this.form.TAX.value =
    (this.form.TAXRATE.value - 0) * (this.form.ITEM1TOTAL.value - 0) +
    (this.form.ITEM2TOTAL.value - 0) " Size="4">

    In my TAX field I get something like 1.7500000000000002.

    Is it possible to format this to read 1.75?

    Also, when summing or multiplying fields, Id like to have 4 x 2.00 = 8.00
    rather than just 8.

    Is this possible?

    Thanks!
    Targa, May 12, 2004
    #1
    1. Advertising

  2. "Targa" <> wrote in message
    news:m8joc.6883$...
    > In my TAX field I get something like 1.7500000000000002.
    >
    > Is it possible to format this to read 1.75?
    >
    > Also, when summing or multiplying fields, Id like to have 4 x 2.00 = 8.00
    > rather than just 8.
    >
    > Is this possible?
    >


    Read the faq: http://jibbering.com/faq/#FAQ4_6


    JW
    Janwillem Borleffs, May 12, 2004
    #2
    1. Advertising

  3. JRS: In article <m8joc.6883$>, seen in
    news:comp.lang.javascript, Targa <> posted at
    Wed, 12 May 2004 01:05:08 :

    ><input NAME="TAXRATE" onBlur="this.form.TAX.value =
    >(this.form.TAXRATE.value - 0) * (this.form.ITEM1TOTAL.value - 0) +
    >(this.form.ITEM2TOTAL.value - 0) " Size="4">


    The first two -0 should be unnecessary, also the associated parentheses.

    this.form.TAX.value =
    this.form.TAXRATE.value * this.form.ITEM1TOTAL.value +
    +this.form.ITEM2TOTAL.value

    should suffice; note the second + and its being not adjacent to the
    first. FAQ 4.21 refers. But your code does what you expect of it,
    except that ISTM that TAX is not the ideal name for the sum.


    >In my TAX field I get something like 1.7500000000000002.
    >
    >Is it possible to format this to read 1.75?


    Yes.


    >Also, when summing or multiplying fields, Id like to have 4 x 2.00 = 8.00
    >rather than just 8.
    >
    >Is this possible?


    No. What you need is possible; what you ask for is not.

    You need to think about the distinction between type Number, which is
    the result of multiplication and is an IEEE Double ( 8 will actually be
    represented along the lines of 1.0000*2^3 ), and type String, which is
    a row of keyboard (and sometimes other) characters. Only a String can
    have trailing zeroes.

    Arithmetic results in type Number; various things, including
    document.write, convert Number into String in a rather simple-minded
    manner.

    Method toFixed converts Number to String more suitably; but it is
    unreliable and may not be available.

    See FAQ 4.6. For a choice of conversions, with tests & testers, see in
    <URL:http://www.merlyn.demon.co.uk/js-round.htm>.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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 12, 2004
    #3
    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. David Harmon
    Replies:
    2
    Views:
    2,896
    cplusplus
    Jun 14, 2006
  2. John Cho
    Replies:
    4
    Views:
    673
    cplusplus
    Jun 14, 2006
  3. Edith Gross
    Replies:
    5
    Views:
    1,104
    Jerry Coffin
    May 1, 2005
  4. Suresh Jeevanandam
    Replies:
    3
    Views:
    1,423
    Tim Hochberg
    Dec 21, 2005
  5. Dave
    Replies:
    1
    Views:
    2,072
Loading...

Share This Page