Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript convert int to float

Tags:

javascript

I have a variable

var fval = 4; 

now I want out put as 4.00

like image 436
Rajesh Avatar asked Oct 30 '10 06:10

Rajesh


People also ask

What does parseFloat do in JavaScript?

The parseFloat function converts its first argument to a string, parses that string as a decimal number literal, then returns a number or NaN .

Can you convert int to float?

Converting Integers to FloatsBy using the float() function, we can convert integers to floats.

Can int be converted to float in Java?

So, when you assign an int value to float variable, the conversion of int to float automatically happens in Java. This is also called widening casting or widening primitive conversion. So, to convert an in to float using widening casting, you can just assign a value of int to float.

How do I float a number in JavaScript?

The parseFloat() function is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.


2 Answers

JavaScript only has a Number type that stores floating point values.

There is no int.

Edit:

If you want to format the number as a string with two digits after the decimal point use:

(4).toFixed(2) 
like image 53
Alex Jasmin Avatar answered Oct 03 '22 07:10

Alex Jasmin


toFixed() method formats a number using fixed-point notation. Read MDN Web Docs for full reference.

var fval = 4;  console.log(fval.toFixed(2)); // prints 4.00 
like image 26
nkshio Avatar answered Oct 03 '22 07:10

nkshio