JavaScript FAQ(五)——数字

本文介绍了JavaScript中数字的多种表示形式,包括常量、不同进制数的使用及转换方法,并提供了数学函数、随机数生成和数值四舍五入等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三、数字

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); // num now holds 255 hexStr='7F'; num = parseInt(hexStr,16); // num now holds 127
parseInt的第二个参数指定原始串中的进制。它的取值范围是2到36的整数。

4. 把数字转化为不同进制(Converting Numbers to Another Base

Q:我怎么样把一个数字转化其他不同进制呢?

A:在JavaScript1.1中,你可以使用标准方法Number.toString(radix)将数字转化为非十进制数字的字符串(例如,二进制、八进制或十六进制的字符串)。例如:

a = (32767).toString(16) // this gives "7fff" b = (255).toString(8) // this gives "377" c = (1295).toString(36) // this gives "zz" d = (127).toString(2) // this gives "1111111"

然而在旧版浏览器(只支持JavaScript1.0)中没有用来转化的标准方法。下面的函数可以在JavaScript1.0将数字转化任意进制的数字:

function toRadix(N,radix) { var HexN="", 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):

  1. -a//changethesignofa
  2. ~a//bitwiseNOTa
  3. ++a//add1toa(beforeusinga)
  4. a++//add1toa(afterusinga)
  5. --a//subtract1froma(beforeusinga)
  6. a--//subtract1froma(afterusinga)

二元操作需要两个参数(下面的例子中参数是a和b):

  1. a*b//multiplyabyb
  2. a/b//divideabyb
  3. a%b//findtheremainderofdivisionofabyb
  4. a+b//addaandb
  5. a-b//subtractbfroma
  6. a&b//bitwiseaANDb
  7. a|b//bitwiseaORb
  8. a^b//bitwiseaXORb

移位操作有:

  1. a<<b//shiftabybbitstotheleft
  2. //(paddingwithzeros)
  3. a>>b//shiftabybbitstotheright
  4. //(copyingthesignbit)
  5. a>>>b//shiftabybbitstotheright
  6. //(paddingwithzeros)

6. 数字 vs. 字符串(Numbers vs. String

Q:是否有方法判断一个变量是数字还是字符串?

A:有。要测试一个变量是数字还是字符串,可以使用typeof操作符。如果变量是数字,typeof(variable)就会返回“number”。如果是字符串,它就会返回“string”。下面是typeof用法的例子:

typeof(123) // result: "number" typeof("123") // result: "string" if (typeof k == "string") { alert('k is a string.') } if (typeof k == "number") { alert('k is a number.') }

typeof操作符也可以帮你区分其他数据类型。根据特定变量的值,typeof的结果可能下列其中之一:

"number"
"string"

"boolean"
"function"
"object"
"undefined"

7. 数学函数(Math Functions

Q:JavaScript支持的数学函数有哪些?

A:JavaScript支持下列数学函数(Math对象的方法):

Math.abs(a) // the absolute value of a Math.acos(a) // arc cosine of a Math.asin(a) // arc sine of a Math.atan(a) // arc tangent of a Math.atan2(a,b) // arc tangent of a/b Math.ceil(a) // integer closest to a and not less than a Math.cos(a) // cosine of a Math.exp(a) // exponent of a Math.floor(a) // integer closest to and not greater than a Math.log(a) // log of a base e Math.max(a,b) // the maximum of a and b Math.min(a,b) // the minimum of a and b Math.pow(a,b) // a to the power b Math.random() // pseudorandom number in the range 0 to 1 Math.round(a) // integer closest to a Math.sin(a) // sine of a Math.sqrt(a) // square root of a Math.tan(a) // tangent of a

注意三角函数都是假设参数是以弧度表示,而不是角度!

8. 随机数(Random Numbers

Q:JavaScript中如何产生随机数?

A:要产生范围在0到1的随机浮点数,可以使用Math.random()方法:

num = Math.random() // num is random, from 0 to 1

如果需要产生范围从A到B的随机浮点数,使用下面的代码:

num = A + (B-A)*Math.random() // num is random, from A to B

9. 精确值(Accuracy

Q:有时候JavaScript计算看起来会产生不精确的结果,例如,0.362 * 100 = 36.199999999999996。我应该如何避免这种情况呢?

A:要避免不精确的情况,你可能想把结果四舍五入到到你使用的数据的精度。例如,为了把结果四舍五入到千分之一,可以使用下面的代码:

  1. roundedX=Math.round(1000*rawX)/1000;

10. 四舍五入(Rounding

Q:我如何把数字四舍五入到n位小数?

A:你可以使用下面的代码来四舍五入:

Math.round(10*X)/10; // round to tenths Math.round(100*X)/100; // round to hundredths Math.round(1000*X)/1000; // round to thousandths ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值