[Javascript] Multiplication

Ben Curtis Quixote at LaMancha.org
Sun Jun 24 20:22:45 CDT 2001


> var myNumber=6*1.5
> 
> that returns 9 but how do I make it 9.00
> 
> or how do I make 3*1.5     4.50  rather than 4.5
> 
> In ASP we would just go formatnumber(myNumber,2)  is there no similar
> function in Javascript?


Not until you write it. :)

I've used something like this before:

function formatnumber(Number, Precision) {
  var P = (Precision) ? Math.pow(10,Precision) : 1;
  var Z = "";
  for (var xx=0; xx<Precision; xx++)
    Z += "" + "0";
  var N = (parseInt(Number * P) / P) + Z;
  return N.substring(0, N.indexOf('.') + Precision +1);
}

I just re-wrote this, so I leave it to you for testing.

+Ben Curtis

"You must be the change you wish to see in the world."
Envision a better world, and live as if it surrounded you.











More information about the Javascript mailing list