homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

a way to limit decimal places with js

 5:59 am on Nov 19, 2002 (gmt 0)

I have no idea, does anyone know of a way to do it?

I have a calculated dollar value that loves having tons of decimal places. In the past, I have always limited the size of the element but is there a better way?



 6:07 am on Nov 19, 2002 (gmt 0)

I used this in a function:


You'd have to add some code after that to add the extra 0's in case it was needed.


 6:19 am on Nov 19, 2002 (gmt 0)

thanks dcheney,

perfect, my js isn't quite up to snuff these days, out of practice. That truncated beautifully. I guess I will have to add the trailing 0's but that can wait until tomorrow.


 1:44 pm on Nov 19, 2002 (gmt 0)

<script type="text/javascript"> 
var olds = '2,346.4673292389';
var news = olds.replace(/^([\d,]+)\.(\d\d)\d*/,"$1.$2");

will work as well.



 4:32 pm on Nov 19, 2002 (gmt 0)

Javascript doesn't have an elegant way of dealing with numbers.

I use a VB script function for this. You can mix the two on your pages.

function DoNumber(str, nDigitsAfterDecimal, nLeadingDigit, nUseParensForNeg, nGroupDigits)
if not isnull(str) then

DoNumber = replace(cstr(FormatNumber(str, nDigitsAfterDecimal, nLeadingDigit, nUseParensForNeg, nGroupDigits)),".",",")
lft = left(donumber,len(donumber) - 3)
rt = right(donumber,2)
donumber = lft & "." & rt
donumber = 0.00
end if
End Function

I left the "lft" and "rt" routines in just in case your DB like to use European formatting for numbers. I'm using mySql and never did figure out how to use US numbers. So I adapted my functions instead.


 4:41 pm on Nov 19, 2002 (gmt 0)

This function will return a 2 place decimal rounded up or down accordingly:

function round(num){
num = num.toString();
var split = num.split(".");
var dec = split[1].substr(0,3);
var dec1 = dec.match(/\d{2,2}/);
var dec2 = dec.match(/(\d$)/);
var rounded = (dec1 + "." + dec2[0]);
return split[0] + "." + Math.round(rounded);


 4:55 pm on Nov 19, 2002 (gmt 0)

I once found a js implementation of printf.

You can take a look at the script translated from Italian here [translate.google.com]

While not perfect, it does give some basic formatting capability:

js> sprintf("%3.4e", 3.345235423545);

I found it floating on the net, so I believe it can be used freely. The authors email is within.


[edited by: heini at 6:56 pm (utc) on Nov. 19, 2002]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved