SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rounding Off values to two decimals

    I am making a script where the tax is calculated on three sets of totals added together. My problem is I need it to be rounded off to cents and I dont know how to do this can someone please help.

    Something like:

    Code:
    tax = Math.round.2decimals((item1 + item2 + item3) * .07)
    Wavelan

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well normally I would write something lengthy with a bit of a tutorial built in. Well I am tired. So here is an excellent tutorial but Vincent (one of our own). Best of luck

    http://members.aol.com/grassblad/html/currency.html
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    perhaps a different way?

    You *could* bounce it off a server side mini script quick.

    ie: (in mason)

    // round.js
    var numb = "<% $numb %>"

    <%args>
    $numb=>undef
    </%args>

    <%init>
    $numb=sprintf("%2f", $numb);
    </%init>

    then in your browser just use a script tag:

    <script language="Javascript"
    src="round.js"
    type="text/javascript"
    id="round"
    onload="rload=true;"></script>

    then when you want to get a new rounded number:

    var de=document.getElementById; // Set getter

    numb=3; // example
    rload=false;
    de('round').src="round.js?numb="+numb;
    while (!rload){
    loop_control++;
    }
    alert(numb);

    I don't know if that's a longer way round.. but i was just thinking "out of the box"

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  4. #4
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So there isnt just any easy way to round off to two decimal places?

    Thats all i want to do there is too much going on for me to understand in that tutorial above
    Wavelan

  5. #5
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Wavelan
    So there isnt just any easy way to round off to two decimal places?

    Thats all i want to do there is too much going on for me to understand in that tutorial above
    Not that I can think of. Personally I would use php which would be fast but I like to use javascript also. I think the script (either one) you coud simply cut and past the code and not learn a thing.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thought there must be a better answer

    Math.round(number*Math.pow(10,2))/Math.pow(10,2);

    Will round to 2 decimal places the variable number.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

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