javascript - Display two decimal places, no rounding -


suppose have value of 15.7784514, want display 15.77 no rounding.

var num = parsefloat(15.7784514); document.write(num.tofixed(1)+"<br />"); document.write(num.tofixed(2)+"<br />"); document.write(num.tofixed(3)+"<br />"); document.write(num.tofixed(10)); 

results in -

15.8 15.78 15.778 15.7784514000  

how display 15.77?

convert number string, match number second decimal place:

var with2decimals = num.tostring().match(/^-?\d+(?:\.\d{0,2})?/)[0] 

the tofixed method fails in cases unlike tostring, careful it.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -