PDA

View Full Version : round of decimal values in float number to 2 digits


phani
07-31-2003, 07:46 AM
Hi,

How to round of the float number to 2 digits in the decimal value of the number. For example if we give 25.3654 then it should be round of to 25.36

phani.

beetle
07-31-2003, 08:13 AM
Well, with that result you're really asking for truncation, so I'll include that as an option.Number.prototype.round = function( places, truncate )
{
var factor = Math.pow( 10, places );
return Math[Boolean(truncate)?'floor':'round']( this * factor ) / factor;
}Then, the usagevar someNum = 25.3654;
var truncatedNum = someNum.round( 2, true );
var roundedNum = someNum.round( 2 );