Click here to Skip to main content
16,007,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m using javascript
how to get 2 values after decimal
(eg)
Its giving me result like this 77.888555
I want like this 77.88

Thanks in advance
Posted

And this[^] never occured to you ?
 
Share this answer
 
Try this

To round off:

Math.round(77.888555*100)/100 ;


Output => 77.89


To truncate try this

var n=77.888555;
alert(truncate(n));

function truncate(num) 
{
	var returnValue=num.toString();
	var pos = returnValue.indexOf(".");	
	if ( pos == -1 )
             {
		return returnValue + ".00";
             }
	else
	{
		returnValue += "00";	
		return returnValue.substr(0,pos+3);
	}
}


Output => 77.88
 
Share this answer
 
v2
JavaScript
number.toFixed(x);

x: Optional. The number of digits after the decimal point. Default is 0 (no digits after the decimal point)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900