三、数字
1. 常量(Constants)
Q:在编写JavaScript代码时,我可以使用什么类型的数字?
A:在JavaScript中,你可以使用下列形式的数字:
- 常规十进制数字:
5 137 1.3
指数形式的十进制数字:6.67e-11 -1.127e20
八进制数,例如:01234 -077 0312
八进制的正数需以0开头,而负数应以-0开头。
十六进制数,例如:0xFF -0xCCFF 0xabcdef
十进制的正数需以0x开头,而负数应以-0x开头。
2. 数学常量(Math Constants)
Q:在JavaScript中,我如何处理数学的常量?
A:一些数学常量在JavaScript中已经预定义了。你可以使用下面的这些常量:
Math.PI // pi = 3.14159265...
Math.E // e = 2.71828182...
Math.LOG2E //以2为底的log
Math.LOG10E //以10为底的log
Math.LN2 // 以e为底的log2
Math.LN10 // 以e为底的log10
Math.SQRT2 // 2的平方根
Math.SQRT1_2 // 1/2的平方根
因此,不需要记忆e或者pi的精确值,只需要Math.E或Math.PI!
3. 八进制和十六进制(Octals and Hexadecimals)
Q:有没有办法可以在JavaScript中使用八进制和十六进制的数字?
A:有的。在JavaScript中可以使用八进制和十六进制数。
八进制数,例如:
01234 -077 0312
八进制的正数需以0开头,而负数应以-0开头。
十六进制数,例如:
0xFF -0xCCFF 0xabcdef
十进制的正数需以0x开头,而负数应以-0x开头。
当需要将八进制或十六进制字符串转化为数字时,使用函数parseInt(str, base)。看下面的例子:
- octalStr='377';
- num=parseInt(octalStr,8);//numnowholds255
- hexStr='7F';
- num=parseInt(hexStr,16);//numnowholds127
parseInt的第二个参数指定原始串中的进制。它的取值范围是2到36的整数。
4. 把数字转化为不同进制(Converting Numbers to Another Base)
Q:我怎么样把一个数字转化其他不同进制呢?
A:在JavaScript1.1中,你可以使用标准方法Number.toString(radix)将数字转化为非十进制数字的字符串(例如,二进制、八进制或十六进制的字符串)。例如:
- a=(32767).toString(16)//thisgives"7fff"
- b=(255).toString(8)//thisgives"377"
- c=(1295).toString(36)//thisgives"zz"
- d=(127).toString(2)//thisgives"1111111"
然而在旧版浏览器(只支持JavaScript1.0)中没有用来转化的标准方法。下面的函数可以在JavaScript1.0将数字转化任意进制的数字:
- functiontoRadix(N,radix){
- varHexN="",Q=Math.floor(Math.abs(N)),R;
- while(true){
- R=Q%radix;
- HexN="0123456789abcdefghijklmnopqrstuvwxyz".charAt(R)+HexN;
- Q=(Q-R)/radix;if(Q==0)break;
- }
- return((N<0)?"-"+HexN:HexN);
- }
可以立即进行测试:
5. 算术操作(Arithmetic Operations)
Q:JavaScript都支持什么算术操作?
A:JavaScript支持下列算术操作(你可以使用括号分组以组成更加复杂的表达式):
- -a//changethesignofa
- ~a//bitwiseNOTa
- ++a//add1toa(beforeusinga)
- a++//add1toa(afterusinga)
- --a//subtract1froma(beforeusinga)
- a--//subtract1froma(afterusinga)
- a*b//multiplyabyb
- a/b//divideabyb
- a%b//findtheremainderofdivisionofabyb
- a+b//addaandb
- a-b//subtractbfroma
- a&b//bitwiseaANDb
- a|b//bitwiseaORb
- a^b//bitwiseaXORb
- a<<b//shiftabybbitstotheleft
- //(paddingwithzeros)
- a>>b//shiftabybbitstotheright
- //(copyingthesignbit)
- a>>>b//shiftabybbitstotheright
- //(paddingwithzeros)
6. 数字 vs. 字符串(Numbers vs. String)
Q:是否有方法判断一个变量是数字还是字符串?
A:有。要测试一个变量是数字还是字符串,可以使用typeof操作符。如果变量是数字,typeof(variable)就会返回“number”。如果是字符串,它就会返回“string”。下面是typeof用法的例子:
- typeof(123)//result:"number"
- typeof("123")//result:"string"
- if(typeofk=="string"){alert('kisastring.')}
- if(typeofk=="number"){alert('kisanumber.')}
typeof操作符也可以帮你区分其他数据类型。根据特定变量的值,typeof的结果可能下列其中之一:
"number""string"
"boolean"
"function"
"object"
"undefined"
7. 数学函数(Math Functions)
Q:JavaScript支持的数学函数有哪些?
A:JavaScript支持下列数学函数(Math对象的方法):
Math.abs(a)//theabsolutevalueofa Math.acos(a)//arccosineofa Math.asin(a)//arcsineofa Math.atan(a)//arctangentofa Math.atan2(a,b)//arctangentofa/b Math.ceil(a)//integerclosesttoaandnotlessthana Math.cos(a)//cosineofa Math.exp(a)//exponentofa Math.floor(a)//integerclosesttoandnotgreaterthana Math.log(a)//logofabasee Math.max(a,b)//themaximumofaandb Math.min(a,b)//theminimumofaandb Math.pow(a,b)//atothepowerb Math.random()//pseudorandomnumberintherange0to1 Math.round(a)//integerclosesttoa Math.sin(a)//sineofa Math.sqrt(a)//squarerootofa Math.tan(a)//tangentofa
注意三角函数都是假设参数是以弧度表示,而不是角度!
8. 随机数(Random Numbers)
Q:JavaScript中如何产生随机数?
A:要产生范围在0到1的随机浮点数,可以使用Math.random()方法:
- num=Math.random()//numisrandom,from0to1
如果需要产生范围从A到B的随机浮点数,使用下面的代码:
- num=A+(B-A)*Math.random()//numisrandom,fromAtoB
9. 精确值(Accuracy)
Q:有时候JavaScript计算看起来会产生不精确的结果,例如,0.362 * 100 = 36.199999999999996。我应该如何避免这种情况呢?
A:要避免不精确的情况,你可能想把结果四舍五入到到你使用的数据的精度。例如,为了把结果四舍五入到千分之一,可以使用下面的代码:
- roundedX=Math.round(1000*rawX)/1000;
10. 四舍五入(Rounding)
Q:我如何把数字四舍五入到n位小数?
A:你可以使用下面的代码来四舍五入:
- Math.round(10*X)/10;//roundtotenths
- Math.round(100*X)/100;//roundtohundredths
- Math.round(1000*X)/1000;//roundtothousandths
- ...