I need a number to be in the form "######.##" (or at least two decimal places). Right now I'm using a convoluted system of ifs to do it, isn't there a better way?
Printable View
I need a number to be in the form "######.##" (or at least two decimal places). Right now I'm using a convoluted system of ifs to do it, isn't there a better way?
here is a rounding 2 to decimal places. not sure if it will help, but might give you some ideas.
EX: 7.9 equals 7.90, or 7.777 equals 7.78, and so on.
Code:<html>
<head>
<title>Untitled</title>
<script language=javascript>
Function roundIt(amount)
{
var s = "";
var Decimal;
amount = parseFloat(amount);
If (!(isNaN(amount))) {
// round to nearest cent
amount = Math.round(amount * 100);
amount = amount / 100;
// format the output
s = New String(amount);
Decimal = s.indexOf(".");
If (Decimal == -1) {
// whole number
s+= ".00";
} Else {
If (Decimal == (s.length - 2)) {
// needs a trailing zero
s+= "0";
}
}
} Else {
// not a number so return zero
s = "0.00";
}
document.form1.Text1.value=s;
//calculate();
Return s;
}
</script>
</head>
<body>
<form Name=form1>
<br><br>
<Input Type=Text Name=Text1 onBlur="roundIt(this.value)"> <Input Type=button value="Round It" onClick="roundIt(document.form1.text1.value)">
<br>
</body>
</html>
Yeah, that's what I was doing (I am having it only go to one place because of how complex it starts to get). I was hoping that something like Math.round() could help. :(
So much for Math.round: http://home.netscape.com/eng/mozilla...ef_r-r.htm#308 :(
BTW, I forgot the most important thing: if it rounds to something like 1.10, 1.00, 3.20, 4.00, etc., I need the trailing zeros. :)
*ahem* :(
dont you mean leading zeros :pQuote:
Originally posted by filburt1
So much for Math.round: http://home.netscape.com/eng/mozilla...ef_r-r.htm#308 :(
BTW, I forgot the most important thing: if it rounds to something like 1.10, 1.00, 3.20, 4.00, etc., I need the trailing zeros. :)
0001 = leading, 1.000 = trailing :)
"######.##"Quote:
Originally posted by filburt1
BTW, I forgot the most important thing: if it rounds to something like 1.10, 1.00, 3.20, 4.00, etc., I need the trailing zeros. :)
ummm, 1.10 and , 1.00 already have the trailing zeros, and you are saying that you need trailing? ummm, i would rather need leading zeros to get ######.##
here try this...
eg. formatNumber(7.777,2) returns "7.78"Code:function formatNumber(expr, decimals) {
var str = "" + Math.round( eval(expr) * Math.pow(10,decimals))
while (str.length <= decimals) { str = "0" + str } // maybe add some leading zeros
var decpoint = str.length - decimals // find location of decimal point
var result = str.substring(0,decpoint);
if (decimals) result += "." + str.substring(decpoint,str.length);
return result;
}
formatNumber(1+1,2) returns "2.00"
formatNumber(20/3,0) returns "7"
At the moment it evaluates an expression as input, but doesn't check for rubbish input, but you could change that by doing:
function formatNumber(num, decimals) {
if (isNaN(parseFloat(num)) return "0";
var str = "" + Math.round( parseFloat(num) * Math.pow(10,decimals))
etc.
Thanks, I'll try it out. :)