Rounding Numbers to a given Decimal Point

For reasons that are beyond me, Javascript does not have a function that could be used to round a number to a given decimal points. If you want to show the price of an item in the format 5.00, you cannot just call a function round(5,2) to do it. Yes, Javascript has a rounding function - Math.round() - but that function just rounds the number to its nearest integer. So, as always, I have created a function that will do this...

I have done many things that I hate to get this function to work - including converting a number to a string. Don't blame me - its the only way. So do not use the returned value for any calculation - use it for display only. If you must use it for calculation, convert it back to a number using the Number() function.

/**
 * Function that could be used to round a number to a given decimal points. Returns the answer
 * Arguments :  number - The number that must be rounded
 *				decimal_points - The number of decimal points that should appear in the result
 */
function roundNumber(number,decimal_points) {
	if(!decimal_points) return Math.round(number);
	if(number == 0) {
		var decimals = "";
		for(var i=0;i<decimal_points;i++) decimals += "0";
		return "0."+decimals;
	}

	var exponent = Math.pow(10,decimal_points);
	var num = Math.round((number * exponent)).toString();
	return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}

//Usage
roundNumber(2.55343,2); //Returns 2.55
roundNumber(2.55843,2); //Returns 2.56
roundNumber(2,2); //Returns 2.00
roundNumber(0,2); //Returns 0.00

Is there a better way to do this? Preferably without using string operations?

Comments

Kumar S at 17 Jan, 2007 06:01



Number.prototype.toFixed = function(fractionDigits)
{
var m = Math.pow(10,fractionDigits);
return Math.round(this*m,0)/m;
}

Example:
(12.3456789).toFixed(5); will return 12.34568
(12.3456789).toFixed(2); will return 12.34


But this will not round off the values as like you are doing . but can be changed to do that.

Thanks for the nice workaround.

Kumar S

Reply to this.
Satish at 13 Sep, 2007 06:55

Thanks for the nice work, saved my time.

Regards,
Satish
Reply to this.
Chitra at 21 Oct, 2007 11:11
Hai,
I want to use this decimal concept in php. where to use the above function (i.e)roundNumber(12.123454,2).


In my php coding,

<?=$file_size;?>,In this file size has 12.567895546 this format. how to display this number in 12.57. plz reply me.
Thank U,
with regards,
Chitra V
Reply to this.
Binny V A at 22 Oct, 2007 04:03
There is a function called number_format in PHP - look it up.
Reply to this.
Anonymous at 21 Nov, 2007 10:50
Nice work binny
Reply to this.
Anonymous at 09 Jan, 2008 06:07
if the five numbers will be given what is the decimal number to the nearest number.
Reply to this.
Anonymous at 13 Feb, 2008 09:03
Lets try to round(2 dig) 613.305 (answer is 613.31) You will see 613.30
Reply to this.
Anonymous at 02 Jul, 2008 06:04
If you multiply 61.23 by 100 THEN Math.round() .. then multiply back ...
Reply to this.
Anonymous at 02 Jul, 2008 06:04
err divide back
Reply to this.
Anonymous at 06 Nov, 2008 11:29
Hello But I still have a problem when we pass a number like 0.12 it showss .12
How can I add 00. befor the decimal point, if someone pass value like 0.1231 it returns .12
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.