[Javascript] 2 Decimal Places in Float

Scott Reynen scott at randomchaos.com
Thu Apr 6 12:36:46 CDT 2006


On Apr 6, 2006, at 12:19 PM, David Stoltz wrote:

>
>
> Hi all,
>
> If I have a statement like:
>
> document.theTimer.cost.value = (parseFloat(myOperator) +
> parseFloat(costToAdd));
>
> And the resulting value is something like:
>
> .02304040948378377
>
> How can I "format" the resulting number to only show 2 decimal places,
> like:
>
> .02

document.theTimer.cost.value = Math.round(  ( parseFloat(myOperator) +
parseFloat(costToAdd) ) * 100 ) / 100;

Peace,
Scott



More information about the Javascript mailing list