[thelist] JavaScript: trim function?

Hassan Schroeder hassan at webtuitive.com
Fri Nov 14 11:12:53 CST 2003


Tom Dell'Aringa wrote:

>>>I have a number like this: 4234.343899238
>>>I want to trim that to just 4234.34. Anyone have a handy function
>>>for this before I re-create the wheel?
>>
>>Math.round() is a built-in JavaScript function ...
> 
> 
> Which gives me 4234...i need the two decimal places...

Right, it rounds to an integer value, so

	myVar = 4234.343899238;
	myPrecision = 2;
	myFactor =  Math.pow(10, myPrecision);
	myRoundedVar =  Math.round(myVar * myFactor) / myFactor;

HTH!
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.





More information about the thelist mailing list