wakk0
asked on
javascript to not drop the zeros after decimal
Zvonko,
how can i get javascript to not drop the zeros behind the decimal points?
how can i get javascript to not drop the zeros behind the decimal points?
ASKER
function dp(cost) {
return (cost*1).toFixed(2);
}
document.calcform.grandtot al.value = dp(((document.calcform.tot al.value*1 ))+((docum ent.calcfo rm.labor.v alue*1))+( (document. calcform.i nspection. value*1))+ ((document .calcform. trash.valu e*1))-((do cument.cal cform.amou nt.value*1 )));
***form****
<tr>
<td width="10%" align="right">
<div class="label">Inspection</ div>
</td>
<td><input type="text" name="trash" value="<?php echo($trash); ?>" size=5 class="one_pixel_border" onChange="javascript: reCalculateValues(this.for m)" /></td> <!--need this value to retain zeros--> </tr>
return (cost*1).toFixed(2);
}
document.calcform.grandtot
***form****
<tr>
<td width="10%" align="right">
<div class="label">Inspection</
</td>
<td><input type="text" name="trash" value="<?php echo($trash); ?>" size=5 class="one_pixel_border" onChange="javascript: reCalculateValues(this.for
function dp(o) {
o.value = (o.value*1).toFixed(2);
}
document.calcform.grandtot al.value = dp(((document.calcform.tot al.value*1 ))+((docum ent.calcfo rm.labor.v alue*1))+( (document. calcform.i nspection. value*1))+ ((document .calcform. trash.valu e*1))-((do cument.cal cform.amou nt.value*1 )));
***form****
<tr>
<td width="10%" align="right">
<div class="label">Inspection</ div>
</td>
<td><input type="text" name="trash" value="<?php echo($trash); ?>" size=5 class="one_pixel_border" onChange="reCalculateValue s(this);" /></td>
o.value = (o.value*1).toFixed(2);
}
document.calcform.grandtot
***form****
<tr>
<td width="10%" align="right">
<div class="label">Inspection</
</td>
<td><input type="text" name="trash" value="<?php echo($trash); ?>" size=5 class="one_pixel_border" onChange="reCalculateValue
ASKER
no that doesn't work...
now it's still dropping the zeros but then when i go to edit my fields it's giving me undefined for my calculations.
now it's still dropping the zeros but then when i go to edit my fields it's giving me undefined for my calculations.
ASKER
you can reference this question.
my trash field is just a number that gets inputed by the user then
js is using that value to do the grandtotal.
when i go into the edit form i would like for it to display as currency.
maybe that should be done with php?
my trash field is just a number that gets inputed by the user then
js is using that value to do the grandtotal.
when i go into the edit form i would like for it to display as currency.
maybe that should be done with php?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for the help...i got it to work with php one more step but easier.
Glad I could help. Thanks for the points!
I am not looking for points here.
I posted my answer for your previous question.
My answer has been proven to work (for last 2+ year), which takes of removing non-numberic from a value, rounding to nearest
decimal place.
<HTML>
<HEAD>
<TITLE>My Current Formatter</TITLE>
<STYLE>
</STYLE>
</HEAD>
<BODY>
<script language="javascript">
function formatCurrency(fld, curPrefix) {
num = fld.value;
//Get rid of any thing other than numbers and decimal places.
num = num.toString().replace(/[^ 0-9.]/g,'' );
// Another safety check.
if(isNaN(num)) num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000 000001);
cents = num%100;
num = Math.floor(num/100).toStri ng();
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));
}
// Comment next line you do not want 2 decimal places.
fld.value = ( ( (sign)?'':'-') + curPrefix + num + '.' + cents);
return (((sign)?'':'-') + curPrefix + num + '.' + cents);
// UnComment next line you do not want 2 decimal places.
//fld.value = (((sign)?'':'-') + curPrefix + num);
//return (((sign)?'':'-') + curPrefix + num);
}
</script>
<br> On Blur formats for currency
<br>Input Amount : <input type="text" onblur="formatCurrency (this,'$');">
</BODY>
</HTML>
I posted my answer for your previous question.
My answer has been proven to work (for last 2+ year), which takes of removing non-numberic from a value, rounding to nearest
decimal place.
<HTML>
<HEAD>
<TITLE>My Current Formatter</TITLE>
<STYLE>
</STYLE>
</HEAD>
<BODY>
<script language="javascript">
function formatCurrency(fld, curPrefix) {
num = fld.value;
//Get rid of any thing other than numbers and decimal places.
num = num.toString().replace(/[^
// Another safety check.
if(isNaN(num)) num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000
cents = num%100;
num = Math.floor(num/100).toStri
if(cents<10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+
num = num.substring(0,num.length
num.substring(num.length-(
}
// Comment next line you do not want 2 decimal places.
fld.value = ( ( (sign)?'':'-') + curPrefix + num + '.' + cents);
return (((sign)?'':'-') + curPrefix + num + '.' + cents);
// UnComment next line you do not want 2 decimal places.
//fld.value = (((sign)?'':'-') + curPrefix + num);
//return (((sign)?'':'-') + curPrefix + num);
}
</script>
<br> On Blur formats for currency
<br>Input Amount : <input type="text" onblur="formatCurrency (this,'$');">
</BODY>
</HTML>
OR
var floatval = value * 1