Link to home
Start Free TrialLog in
Avatar of wakk0
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?
Avatar of ic3b3rg
ic3b3rg

var floatval = parseFloat(value)

OR

var floatval = value * 1
Avatar of wakk0

ASKER

function dp(cost) {
  return (cost*1).toFixed(2);
}

document.calcform.grandtotal.value = dp(((document.calcform.total.value*1))+((document.calcform.labor.value*1))+((document.calcform.inspection.value*1))+((document.calcform.trash.value*1))-((document.calcform.amount.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.form)" /></td>      <!--need this value to retain zeros-->                        </tr>
function dp(o) {
  o.value = (o.value*1).toFixed(2);
}

document.calcform.grandtotal.value = dp(((document.calcform.total.value*1))+((document.calcform.labor.value*1))+((document.calcform.inspection.value*1))+((document.calcform.trash.value*1))-((document.calcform.amount.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="reCalculateValues(this);" /></td>
Avatar of wakk0

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.
Avatar of wakk0

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?
ASKER CERTIFIED SOLUTION
Avatar of ic3b3rg
ic3b3rg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wakk0

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!
Avatar of Pravin Asar
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.50000000001);
   cents = num%100;
   num = Math.floor(num/100).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));
   }

   
   // 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>