JavaScript Tutorial/Number Data Type/Float

Материал из Web эксперт
Перейти к: навигация, поиск

Adding the float point numbers together

   <source lang="javascript">

<html> <head> <title>GoodAdd</title> </head> <body> <script> var meal; meal = 123.45; meal = eval(meal); var tip = meal * .15; var total = meal + tip; alert ("the meal is $" + meal); alert ("the tip is $" + tip); alert ("Total bill: $" + total); </script> </body> </html></source>


Floating-Point Numbers

Floating-point numbers can contain fractional parts.

Floating-point numbers can use exponential notation for added precision.

Floating-point numbers are made up of a decimal integer followed by a period (.) and the fractional portion of the number.

Exponential notation can be used by adding an e or E to the end of a floating-point number followed by a decimal integer that does not exceed 3 digits.

Use 1.0 not 1.



   <source lang="javascript">

var fNum = 1.0;</source>


Floating-point values can be represented using e-notation

In e-notation, a number is represented by digits, followed by an e or an E, followed by the number of times to multiply it by 10.



   <source lang="javascript">

var fNum = 3.125e7;</source>


This notation represents the number 31250000.

You can get this value by converting the e-notation to a calculation: 3.125 * 10^7.

0.00000000000000003 can be written as 3e-17.

Here, 10 is raised to the -17 power.

By default, JavaScript converts any floating-point number with six or more leading zeros into e-notation.

Floating-point values are stored in a 64-bit IEEE 754 format.

Decimal values can have up to 17 decimal places.

After that, the values are truncated.

float point value

   <source lang="javascript">

<HTML> <BODY> <SCRIPT language="JavaScript">

</SCRIPT> </BODY> </HTML></source>