当前位置: 代码迷 >> JavaScript >> JS-数目字类型
  详细解决方案

JS-数目字类型

热度:247   发布时间:2013-03-25 15:43:04.0
JS-数字类型

?

Integer Literals:base-10 integer literals、base-16 integer literals、base-8 integer literals?

var a = 23;  
var b = 0x1A;//hexadecimal
var c = 012;//octal

console.log('a='  + a + ',b=' + b + ',c=' +c );
//a=10,b=26,c=10 

?Since some implementations support octal literals and some don't , you should never write an integer literal with a leading zero;you cannot know in this case whether an implementation will interpret it as an octal or decimal value.In the strict mode of ECMASCRIPT5,octal literals are explicitly forbidden.

?

Floating-Point Literals:integral part、decimal point、fractional part.May also be represented using exponential notation:a real number followed by the letter e(E),followed by optional plus or minus sign,followed by an integer exponent.?

console.log(3.14);
console.log('3.14e-2=' + 3.14e-2);
//3.14
//3.14e2=0.0314
var a=38.8;
var b=6.8;  
console.log('(a-b)=' + (a-b));  
var a=134.22;  
var b=6;  
console.log('(a*b)=' + a*b);
//(a-b)=31.999999999999996
//(a*b)=805.3199999999999 

Solution 1:toFixed(),指定小数的精度

console.log('(a-b)=' + (a-b).toFixed(2));  
console.log('(a*b)=' + (a*b).toFixed(2));
//(a-b)=32.00
//(a*b)=805.32 

Solution 2:小数点转成整数运算后再做整数的降级,实际这种方法还是有问题

console.log(Number(-33.34).add(1.77))
//-31.570000000000004

?Reason:there are infinitely many real numbers, but only a finite number of them can be represented exactly by the JavaScript floating-point format.This means that when you're working with real numbers in JavaScript, the representation of the number will often be an approximation of the actual number.

?
?Because of rounding error, the difference between the approximations of .3 and .2 is not exactly the same as the difference between the approximations of .2 and .1.It is important to understand that this problem is not specific to JavaScript:it affects any programming languages that uses binary floating-point numbers.

A future version of JavaScript may support a decimal numeric type that avoids these rounding issues.Until then you might want to perform critical financial calculations using scaled integers.For example, you might manipulate monetary values as integer cents rather than fractional dollars.

?

?Arithmetic:Javascript programs work with numbers using the arithmetic operators.In addition to these basic arithmetic operators, it supports more complex mathematical operations through a set of functions and constants defined as properties of Math object.For example:

Arithmetic in JavaScript doen not raise errors in cases of overflow, underflow, or division by zero.

  1. "negative zero" is almost completely indistinguishable from regular zero and JavaScript programmers rarely need to detect it.
  2. Division by zero is not an error in JS:it simply returns infinity or negative infinity.there is one exception, however:zero divided by zero does not hava a well-defined value,the result of this operation is the special not-a-number value, printed as NaN.?
Number.POSITIVE_INFINITY
Number.MAX_VALUE
Number.NaN
Number.NEGATIVE_INFINITY
Number.MIN_VALUE
//
Infinity,-Infinity,NaN
//
console.log(isNaN(111))
console.log(isNaN(NaN))
console.log(isFinite(Number.POSITIVE_INFINITY))
console.log(Number.POSITIVE_INFINITY == Infinity)
?ECMAScript3,Infinity and NaN are read/write values and can be changed.ECMAScript5 corrects this and makes the values read-only.The Number object defines alternatives that are read-only even in ECMAScript3.?

?

?

  相关解决方案