Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Decimal place into number with javascript

I've got this number as a integer 439980

and I'd like to place a decimal place in 2 places from the right. to make it 4399.80

the number of characters can change any time, so i always need it to be 2 decimal places from the right.

how would I go about this?

thanks

like image 634
owenmelbz Avatar asked Jul 17 '12 14:07

owenmelbz


People also ask

How do I get 2 decimal places in JavaScript?

Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result.

How do you write decimals in JavaScript?

JavaScript Number toFixed() The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.

What does toFixed 0 do in JavaScript?

Description. toFixed() returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

Does JavaScript toFixed round?

Note. The toFixed() method will round the resulting value if necessary. The toFixed() method will pad the resulting value with 0's if there are not enough decimal places in the original number. The toFixed() method does not change the value of the original number.


1 Answers

function insertDecimal(num) {    return (num / 100).toFixed(2); } 
like image 168
nnnnnn Avatar answered Sep 26 '22 04:09

nnnnnn