Can I round or set the number of decimals or even make this a fraction?

Discussion in 'Javascript' started by Schklerg, Nov 25, 2003.

  1. Schklerg

    Schklerg Guest

    I have this script performing a calculation on a page on my site:
    <script language="JavaScript">
    function compute_weight(form)
    {
    var weight = form.wt.value;
    var pgs = form.pgs.value;
    var ppi = form.ppi.value;
    var hite = form.ht.value;
    if (weight > 0.0) {
    form.totwid.value = int_zero( weight * 2 )+(pgs/ppi)+0.5;
    form.totht.value = int_zero( hite )+ 0.5;
    form.spine.value = int_zero( pgs)/ (ppi);
    form.hite1.value = int_zero(hite);
    form.hite2.value = int_zero(hite);
    form.wid1.value = int_zero(weight);
    form.wid2.value = int_zero(weight);
    form.spinetype.value = int_zero( pgs)/ (ppi) -.125;
    }
    }
    // Function to return 0 if result is <1
    function int_zero(x)
    {
    if ( x < 1 )
    return 0 ;
    else
    return parseInt( x ,10 );
    }
    </script>
    At times, depending on the values entereed into my form, I can end up with a
    number like 13.011945392491468. 15 decimal places is just too confusing.
    Is there a way I can round this to 3? Or even better, is there away to make
    this value appear as a fraction? If you need more code from the page, it's
    at www.integratedbook.com/pbkcalc.html. Thanks for your help.

    --
    Jason
    Remove nospam for email replies
    Schklerg, Nov 25, 2003
    #1
    1. Advertising

  2. Schklerg wrote on 25 Nov 2003:

    > I have this script performing a calculation on a page on my
    > site:
    > <script language="JavaScript">


    You must specify the type attribute (type="text/javascript") in the
    SCRIPT element. Type is sufficient and language is not needed.

    > function compute_weight(form)
    > {
    > var weight = form.wt.value;


    Using the elements collection is more cross-browser compatible:

    var = weight = form.elements['wt'].value;

    <snip>

    > At times, depending on the values entereed into my form, I can
    > end up with a number like 13.011945392491468. 15 decimal places
    > is just too confusing. Is there a way I can round this to 3? Or
    > even better, is there away to make this value appear as a
    > fraction?


    You could do this to round to 3dp:

    number *= 1000; // 13.011945392491468 -> 13011.945392491468
    number = Math.round( number ); // 13011.945392491468 -> 13012.0
    number /= 1000; // 13012.0 -> 13.012

    or

    number = Math.round( number * 1000 ) / 1000;

    Displaying a fraction is much more difficult. After looking at your
    site, I don't think it would be particularly appropriate, either.

    Mike

    --
    Michael Winter
    d (remove ".invalid" to reply)
    Michael Winter, Nov 25, 2003
    #2
    1. Advertising

  3. Schklerg

    DU Guest

    Re: Can I round or set the number of decimals or even make this afraction?

    Michael Winter wrote:
    > Schklerg wrote on 25 Nov 2003:
    >
    >
    >>I have this script performing a calculation on a page on my
    >>site:
    >> <script language="JavaScript">

    >
    >
    > You must specify the type attribute (type="text/javascript") in the
    > SCRIPT element. Type is sufficient and language is not needed.
    >
    >
    >>function compute_weight(form)
    >>{
    >> var weight = form.wt.value;

    >
    >
    > Using the elements collection is more cross-browser compatible:
    >
    > var = weight = form.elements['wt'].value;
    >
    > <snip>
    >
    >>At times, depending on the values entereed into my form, I can
    >>end up with a number like 13.011945392491468. 15 decimal places
    >>is just too confusing. Is there a way I can round this to 3? Or
    >>even better, is there away to make this value appear as a
    >>fraction?

    >
    >
    > You could do this to round to 3dp:
    >
    > number *= 1000; // 13.011945392491468 -> 13011.945392491468
    > number = Math.round( number ); // 13011.945392491468 -> 13012.0
    > number /= 1000; // 13012.0 -> 13.012
    >
    > or
    >
    > number = Math.round( number * 1000 ) / 1000;
    >


    or use toPrecision method which involves only 1 call, 1 line:

    "toPrecision

    Returns a string representing the Number object to the specified precision.
    "
    http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1201389

    Also, you may want to look at toFixed
    http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1200964


    DU
    DU, Nov 25, 2003
    #3
  4. Schklerg

    PatD Guest

    On Tue, 25 Nov 2003 08:25:54 -0500, "Schklerg"
    <> wrote:

    >I have this script performing a calculation on a page on my site:

    [cut]
    >At times, depending on the values entereed into my form, I can end up with a
    >number like 13.011945392491468. 15 decimal places is just too confusing.
    >Is there a way I can round this to 3? Or even better, is there away to make
    >this value appear as a fraction?


    Fractions? Well... depends on your numbers and the precision required.

    Your 13.011945392491468 is "close enough" approximated by 7625 / 586.

    Now, whether that's good enough for you,
    or, maybe, the numbers are "too large" already?


    Anyway, you may want to have a look at this:

    function q(n)
    {
    var x = n;
    var a = Math.floor(n);
    var p0 = 1;
    var q0 = 0;
    var p1 = a;
    var q1 = 1;
    var p2 = p1;
    var q2 = q1;

    while(x - a && Math.abs(n - p2 / q2) > 1e-10)
    {
    x = 1 / (x - a);
    a = Math.floor(x);
    p2 = a * p1 + p0;
    q2 = a * q1 + q0;
    p0 = p1;
    q0 = q1;
    p1 = p2;
    q1 = q2;
    }
    return p2 + " / " + q2;
    }


    Should return a fractional "string" of anything you might want
    to throw at it :)

    Constructive comments welcome.


    Yours
    P
    PatD, Nov 25, 2003
    #4
  5. Schklerg

    Schklerg Guest

    Thanks. That's just what I needed.

    --
    Jason
    Remove nospam for email replies

    "DU" <> wrote in message
    news:bpvn31$alo$...
    > Michael Winter wrote:
    > > Schklerg wrote on 25 Nov 2003:
    > >
    > >
    > >>I have this script performing a calculation on a page on my
    > >>site:
    > >> <script language="JavaScript">

    > >
    > >
    > > You must specify the type attribute (type="text/javascript") in the
    > > SCRIPT element. Type is sufficient and language is not needed.
    > >
    > >
    > >>function compute_weight(form)
    > >>{
    > >> var weight = form.wt.value;

    > >
    > >
    > > Using the elements collection is more cross-browser compatible:
    > >
    > > var = weight = form.elements['wt'].value;
    > >
    > > <snip>
    > >
    > >>At times, depending on the values entereed into my form, I can
    > >>end up with a number like 13.011945392491468. 15 decimal places
    > >>is just too confusing. Is there a way I can round this to 3? Or
    > >>even better, is there away to make this value appear as a
    > >>fraction?

    > >
    > >
    > > You could do this to round to 3dp:
    > >
    > > number *= 1000; // 13.011945392491468 -> 13011.945392491468
    > > number = Math.round( number ); // 13011.945392491468 -> 13012.0
    > > number /= 1000; // 13012.0 -> 13.012
    > >
    > > or
    > >
    > > number = Math.round( number * 1000 ) / 1000;
    > >

    >
    > or use toPrecision method which involves only 1 call, 1 line:
    >
    > "toPrecision
    >
    > Returns a string representing the Number object to the specified

    precision.
    > "
    >

    http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1201389
    >
    > Also, you may want to look at toFixed
    >

    http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1200964
    >
    >
    > DU
    >
    Schklerg, Nov 25, 2003
    #5
  6. JRS: In article <Xns943E8C403462EMWinterBlueyonder@193.38.113.46>, seen
    in news:comp.lang.javascript, Michael Winter <.
    invalid> posted at Tue, 25 Nov 2003 13:47:14 :-
    >
    >You could do this to round to 3dp:
    >
    >number *= 1000; // 13.011945392491468 -> 13011.945392491468
    >number = Math.round( number ); // 13011.945392491468 -> 13012.0
    >number /= 1000; // 13012.0 -> 13.012
    >
    >or
    >
    >number = Math.round( number * 1000 ) / 1000;
    >


    Those round to a multiple of 0.001, which is not exactly the same thing
    as rounding to three decimal places.

    The OP is clearly outputting the number for eyeball input, in other
    words to a string. One-and-a-tenth, rounded to a multiple of 0.001, is a
    number of value (close to) 1.1; but, as a string to three decimal
    places, it must be "1.100".

    FAQ, section 4.6,
    and <URL:http://www.merlyn.demon.co.uk/js-round.htm>

    Note : toFixed and toPrecision are not available in all Web browsers,
    and toFixed (at least) has bugs.

    --
    © 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> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
    Dr John Stockton, Nov 25, 2003
    #6
  7. Schklerg

    PatD Guest

    Albert Wagner <> wrote in message news:<>...
    > On Tue, 25 Nov 2003 15:54:55 +0100
    > PatD <> wrote:
    > I've personally always suffered from math phobia. But, somewhat OT, are
    > there good reasons for using only rational numbers in a given
    > calculation? I remember that the first versions of Smalltalk had only
    > rationals builtin.


    Don't know for the "good" reasons, but, as an example,
    0.14285714285714285 might talk to you, it doesn't to me.
    "1 / 7" however is clearer... YMMV :)


    Yours
    P
    PatD, Nov 26, 2003
    #7
  8. Re: Can I round or set the number of decimals or even make this afraction?

    PatD wrote:

    >Albert Wagner <> wrote in message news:<>...
    >
    >
    >>On Tue, 25 Nov 2003 15:54:55 +0100
    >>PatD <> wrote:
    >>I've personally always suffered from math phobia. But, somewhat OT, are
    >>there good reasons for using only rational numbers in a given
    >>calculation? I remember that the first versions of Smalltalk had only
    >>rationals builtin.
    >>
    >>

    >Don't know for the "good" reasons, but, as an example,
    >0.14285714285714285 might talk to you, it doesn't to me.
    >"1 / 7" however is clearer... YMMV :)
    >Yours
    >P
    >


    This works for me:

    dropletcost = (dropletcost).toFixed(2);
    Mike Scirocco, Nov 30, 2003
    #8
    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. Peter

    String Fraction to Number

    Peter, May 6, 2004, in forum: Java
    Replies:
    6
    Views:
    1,044
    Mason Bryant
    May 6, 2004
  2. Jerry

    Round Decimals

    Jerry, Aug 9, 2005, in forum: Java
    Replies:
    9
    Views:
    7,057
  3. Replies:
    0
    Views:
    1,084
  4. OliverMarchand

    Float.round - should it be round-to-even

    OliverMarchand, Apr 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    218
    OliverMarchand
    Apr 12, 2006
  5. cerr

    reading file round and round

    cerr, Mar 19, 2010, in forum: Perl Misc
    Replies:
    6
    Views:
    175
    Peter J. Holzer
    Mar 20, 2010
Loading...

Share This Page