# Thread: Rounding Off values to two decimals

1. ## 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)

2. 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

3. ## 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"

then when you want to get a new rounded number:

var de=document.getElementById; // Set getter

numb=3; // example
de('round').src="round.js?numb="+numb;
loop_control++;
}

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

Flawless

4. 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

5. 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.

6. ## 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

#### Posting Permissions

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