Minggu, 30 September 2012

JavaScript Numbers

JavaScript Numbers

JavaScript numbers can be written with, or without decimals:

Example

var pi=3.14;    // Written with decimals
var x=34;       // Written without decimals
Extra large or extra small numbers can be written with scientific (exponent) notation:

Example

var y=123e5;    // 12300000
var z=123e-5;   // 0.00123


All JavaScript Numbers are 64-bit

JavaScript is not a typed language. Unlike many other programming languages, it does not define different types of numbers, like integers, short, long, floating-point etc.
All numbers in JavaScript are stored as 64-bit (8-bytes) base 10, floating point numbers.

Precision

Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits.
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:

Example

var x=0.2+0.1;



Octal and Hexadecimal

JavaScript interprets numeric constants as octal if they are preceded by a zero, and as hexadecimal if they are preceded by a zero and and x.

Example

var y=0377;
var z=0xFF;

lamp Never write a number with a leading zero, unless you want an octal conversion. 

0 komentar:

Posting Komentar