Search This Blog

Sunday, September 18, 2016

JavaScript Equivalent of PHP's number_format() Function

Today while going through JavaScript I came across a float value and then wanted to truncate it by two decimal places. I knew in PHP we have the number_format() function and this is what I found for Javascript:

yourFloatVarHere.toFixed(2);

Example:

Suppose you have a value say: 0.589723, and you would like to truncate this value to two decimal place.
 var divide;  
 var num_format;  
 divide = 33/86;  
 num_format = divide.toFixed(2);  

33/ 8 = 0.38372093
Since we have truncated our answer to two decimal place, the final answer will be: 0.38

No comments:

Post a Comment

Thank you for your Feedback!
www.evagabond.me

Top Magento Hosting:

Top 5 Posts (Weekly)