[thelist] Calculations in JS

Shashank Tripathi sub at shanx.com
Sun Jul 21 07:33:01 CDT 2002


Hi Adrian,


    | In JS I'm doing some calculations and I need to shorten
    | the result down to 2 decimals.  The number I am
    | multiplying by is 0.00833333333333 ( could round up to
    | 0.834)  but I don't want to display a gazillion places.


Not sure if that is a typo, but 0.008333 rounded will not give you
0.834, it will give you 0.01. I guess you added a couple of zeroes after
the decimal in your original number so in the examples below, I will
assume the original number to be 0.83333...


    // Original number truncated to 1 decimal
    var orig = 0.00833333333333;
    var truncated = Math.round(orig*10)/10;   // gives you 0.8


    // Original number truncated to 2 decimals
    var orig = 0.833333333333;
    var truncated = Math.round(orig*100)/100;   // gives you 0.83


If 3 decimals, then replace 100 by 1000...and so on.



    | with close button or by Xing the window) can I save the
    | updated variable to a cookie?


You need to know how to set and get cookies. Just google for "javascript
setcookie" etc and there will be tons of it.

I might stand corrected, but as long as the cookie is being set from
your domain name, it should be accessible by server side scripts as
well. So your Perl could set a cookie and JS could access it.

Cheers,
Shanx


--
Shashank Tripathi
www.shanx.com





More information about the thelist mailing list