|
-
Nov 1st, 2001, 03:00 PM
#1
Thread Starter
Member
Equivalent of Format$ in Javascript?
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?
-
Nov 1st, 2001, 03:09 PM
#2
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>
-
Nov 1st, 2001, 03:10 PM
#3
Thread Starter
Member
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.
-
Nov 1st, 2001, 03:14 PM
#4
Thread Starter
Member
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.
-
Nov 2nd, 2001, 12:15 PM
#5
Thread Starter
Member
*ahem*
-
Nov 2nd, 2001, 03:28 PM
#6
dont you mean leading zeros
-
Nov 2nd, 2001, 04:18 PM
#7
Thread Starter
Member
0001 = leading, 1.000 = trailing
-
Nov 5th, 2001, 04:11 AM
#8
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 ######.##
-
Nov 6th, 2001, 10:26 PM
#9
here try this...
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;
}
eg. formatNumber(7.777,2) returns "7.78"
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.
-
Nov 7th, 2001, 05:51 PM
#10
Thread Starter
Member
Thanks, I'll try it out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|