Rounding method??

1. Tal
Senior Member
Join Date
Dec 1969
Posts
100

## Rounding method??

Anybody know a javascript method that will round a number like 23 to 2 or 36 to 4? Basically rounding an integer then pulling the first integer. Hmm..<BR><BR>Thanks

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Math.round( n / 10.0 ) <eop>

.

3. Tal
Senior Member
Join Date
Dec 1969
Posts
100

## Working on this..

this is what I&#039;ve got, but can&#039;t get it to round to the nearest tenth place. Like \$101.56 should round to \$101.6<BR><BR>function currency(num) <BR>{<BR> num = num.toString().replace(/\$&#124,/g,&#039;&#039;);<BR> if(isNaN(num))<BR> num = "0";<BR> var prefix = "\$"; <BR> var suffix = ""; <BR> if ( num &#060; 0 ) <BR> { <BR> prefix = "(\$"; <BR> suffix = ")"; <BR> num = - num; <BR> } <BR> sign = (num == (num = Math.abs(num)));<BR> num = Math.floor(num*100+0.50000000001);<BR> cents = num%100;<BR> num = Math.floor(num/100).toString();<BR> if(cents&#060;10)<BR> cents = "0" + cents; <BR> for (var i = 0; i &#060; Math.floor((num.length-(1+i))/3); i++) <BR> num = num.substring(0,num.length-(4*i+3))+&#039;,&#039;+<BR> num.substring(num.length-(4*i+3));<BR> return (((sign)?&#039;&#039;:&#039;-&#039;) + prefix + num + &#039;.&#039; + cents + suffix);<BR>}

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## What country is this for?

Interesting, a currency that uses only 1 digit after the decimal point.<BR><BR>Anyway, you are working *WAY* to hard:<BR><BR>function currencyOne( num )<BR>{<BR>&nbsp; &nbsp; // ... put in your replace code if you want it here!<BR>&nbsp; &nbsp; num = parseFloat( num );<BR>&nbsp; &nbsp; var prefix = ( num &#060; 0 ) ? "-\$" : "\$";<BR>&nbsp; &nbsp; num = Math.round( 10 * Math.abs(num) ) ); <BR>&nbsp; &nbsp; // if 0 to 9, result is \$0.N (maybe with minus)<BR>&nbsp; &nbsp; if ( num &#060; 10 ) return prefix + "0." + num;<BR><BR>&nbsp; &nbsp; // 10 or more (originally 1.0 or more)<BR>&nbsp; &nbsp; num = prefix + num; // converts it to string<BR>&nbsp; &nbsp; return num.substring(0,num.length-1) + "." + num.substring(num.length-1);<BR>}<BR><BR>**************************<BR><BR> If you use a currency with two digits after the decimal point:<BR><BR><BR>function currencyTwo( num )<BR>{<BR>&nbsp; &nbsp; // ... put in your replace code if you want it here!<BR>&nbsp; &nbsp; num = parseFloat( num );<BR>&nbsp; &nbsp; var prefix = ( num &#060; 0 ) ? "-\$" : "\$";<BR>&nbsp; &nbsp; num = Math.round( 100 * Math.abs(num) ) ); <BR>&nbsp; &nbsp; // if 0 to 9, result is \$0.0N (maybe with minus)<BR>&nbsp; &nbsp; if ( num &#060; 10 ) return prefix + "0.0" + num;<BR>&nbsp; &nbsp; // if 10 to 99, result is \$0.NN (maybe with minus)<BR>&nbsp; &nbsp; if ( num &#060; 100 ) return prefix + "0." + num;<BR><BR>&nbsp; &nbsp; // 100 or more (originally 1.0 or more)<BR>&nbsp; &nbsp; num = prefix + num; // converts it to string<BR>&nbsp; &nbsp; return num.substring(0,num.length-2) + "." + num.substring(num.length-2);<BR>}<BR><BR>

5. Tal
Senior Member
Join Date
Dec 1969
Posts
100

## Pentagon...

Thanks Bill, looks like I made it harder than it appears.<BR><BR>Crazy user requirements. I&#039;ve never heard of rounding to one cent.<BR><BR>Thanks again!!

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not rounding to one CENT!

What you are doing is rounding to the nearest DIME!<BR><BR>If your requirements were to round to the CENT, then you are doing it wrong! Use my "currencyTwo" code, instead.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not rounding to one CENT!

What you are doing is rounding to the nearest DIME!<BR><BR>If your requirements were to round to the CENT, then you are doing it wrong! Use my "currencyTwo" code, instead.<BR><BR>

#### Posting Permissions

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