View Full Version : cutting float

01-29-2006, 02:46 AM
i need to get a price from a user.
the thing is, if he prints 15.545689 i want to get the number with only two digits after the floating point. so the number above will result 15.54

01-29-2006, 03:03 AM
<script language="javascript">

function moneyFmt(thisNumber){
for (i=0; i<thisNumber.length; i++)
if (thisNumber.charAt(i)==".")
return thisNumber.substring(0, i+3)


<form name="cartform">
<input type="text" name="total">
<input type="button" onclick="alert(moneyFmt(this.form.total.value));>

01-30-2006, 08:02 AM
You need to simply use the built-in numberVar.toFixed(2) method.
But it is incorrectly implemented in some browsers or not implemented at all.
liorean prototyped a similar cross-browser method which can be found in the JS FAQ (http://www.codingforums.com/showthread.php?p=178077#post178077).