View Full Version : Need Javascript Help
foiledagain
09-12-2001, 10:32 AM
I have designed a page where after a user selects an item from a select box and enters in a quantity, both of the values are multiplied and a total is shown. It's working fine, except for one thing: I cannot get it to show decimal places. I have tried parseDec, parseFloat, and parseMoney, but for some reason it's just not working and I can't figure out what I'm doing wrong. Here's the part of the script I'm talking about, in it's original form:
function updatePrice() {
var _v = document.upsell.ItemNumber.selectedIndex;
var _x = document.upsell.ItemNumber[_v].value;
var _y = eval(_x);
var _w = document.upsell.Quantity.value;
document.upsell.DollarsEarned.value=prices[_y] * [_w}];
How can I fix this so the total displays the way I want?
Thanks in advance for your help,
foiledagain
Dr. Web
09-12-2001, 12:00 PM
it looks like you are doing math on strings. You still need to parse the strings into integers before doing the math.
var _v = document.upsell.ItemNumber.selectedIndex;
var _x = parseInt(document.upsell.ItemNumber[_v].value);
var _y = eval(_x);
var _w = parseInt(document.upsell.Quantity.value);
document.upsell.DollarsEarned.value=prices[_y] * [_w}];
obviously I can't test your without the whole page, but I wrote an online mortgage calculator which rounds down to two decimal places quite nicely-by using this same method.
you might also need a javascript to add in zeros for loks sake. EX: 7.9 equals 7.90, or 7.777 equals 7.78, and so on. Let me look for the JS for that.
Dr. Web
09-12-2001, 12:05 PM
ok, here is the JS for rounding to two decimals. It doesn't check for numerics... but you most likely have that aready.
<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>
foiledagain
09-12-2001, 03:07 PM
Thanks very much for your help. I'm still having some troubles, but will keep cracking at it.
foiledagain
Originally posted by Dr. Web
ok, here is the JS for rounding to two decimals. It doesn't check for numerics... but you most likely have that aready.
<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>
Jon Hanlon
09-12-2001, 07:57 PM
Numbers in Javascript (and Java) are the IEEE 754 double-precision 64-bit standard (ANSI/IEEE Std 754-1985).
Well and good, but this means that, say, 30.46 - 9.96 = 20.499999999999993 , so you have to write formatting functions like the ones below.
Note that IE5.5 adds several methods to the Number object, including toFixed() and toPrecision(), but it's too early to use these in production unless you're sure of the target platform.
function formatNumber(expr, decimals) {
var str = "" + Math.round( eval(expr) * Math.pow(10,decimals))
while (str.length <= decimals) { str = "0" + str } // maybe add some l
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;
}
function dollarizeNumber(expr) {
return "$" + formatNumber(expr,2)
}
function formatCurrency(inAmount) {
var num = inAmount.toString().replace(/\$|\,|\s/g,'');
if (isNaN(num)) num = "0";
var cents = Math.floor((num*100+0.5)%100);
num = Math.floor(num).toString();
if (cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ','
+ num.substring(num.length-(4*i+3));
return ('$' + num + '.' + cents);
}
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.