How to use float type value in Javascript

Description

To define a floating-point value, you must include a decimal point and at least one number after the decimal point.

An integer is required before a decimal point, but it is recommended.

Example

Here are some examples:


var floatNum1 = 1.1;
console.log(floatNum1);/*from  w ww .  ja  va  2 s  .c o m*/
var floatNum2 = 0.1;
console.log(floatNum2);
var floatNum3 = .1;     //valid, but not recommended
console.log(floatNum3);

The code above generates the following result.

Note

When there is no digit after the decimal point, the number becomes an integer.

If the number being represented is a whole number (such as 1.0), it will be converted into an integer, as in this example:


var floatNum1 = 1.;     //missing digit after decimal - interpreted as integer 1
console.log(floatNum1);
var floatNum2 = 10.0;   //whole number - interpreted as integer 10
console.log(floatNum2);




















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions