JavaScript参考:编号

This article documents how to work with the Number built-in object, and lists its properties and methods.

本文介绍如何使用Number内置对象,并列出其属性和方法。

A number value can be generated using a number literal syntax:

可以使用数字文字语法生成number值:

const age = 36
typeof age //number

or using the Number global function:

或使用Number全局功能:

const age = Number(36)
typeof age //number

If we add the new keyword, we get a Number object in return:

如果添加new关键字,则会得到一个Number对象作为回报:

const age = new Number(36)
typeof age //object

which has a very different behavior than a number type. You can get the original number value using the valueOf() method:

number类型的行为截然不同。 您可以使用valueOf()方法获取原始number值:

const age = new Number(36)
typeof age //object
age.valueOf() //36

物产 (Properties)

  • EPSILON the smallest interval between two numbers

    EPSILON两个数字之间的最小间隔

  • MAX_SAFE_INTEGER the maximum integer value JavaScript can represent

    MAX_SAFE_INTEGER JavaScript可以表示的最大整数

  • MAX_VALUE the maximum positive value JavaScript can represent

    MAX_VALUE JavaScript可以代表的最大正值

  • MIN_SAFE_INTEGER the minimum integer value JavaScript can represent

    MIN_SAFE_INTEGER JavaScript可以表示的最小整数

  • MIN_VALUE the minimum positive value JavaScript can represent

    MIN_VALUE JavaScript可以代表的最小正值

  • NaN a special value representing “not a number”

    NaN代表“不是数字”的特殊值

  • NEGATIVE_INFINITY a special value representing negative infinity

    NEGATIVE_INFINITY代表负无穷大的特殊值

  • POSITIVE_INFINITY a special value representing positive infinity

    POSITIVE_INFINITY代表正无穷大的特殊值

Those properties evaluated to the values listed below:

这些属性评估为以下列出的值:

Number.EPSILON
Number.MAX_SAFE_INTEGER
Number.MAX_VALUE
Number.MIN_SAFE_INTEGER
Number.MIN_VALUE
Number.NaN
Number.NEGATIVE_INFINITY
Number.POSITIVE_INFINITY
2.220446049250313e-16
9007199254740991
1.7976931348623157e+308
-9007199254740991
5e-324
NaN
-Infinity
Infinity

对象方法 (Object Methods)

We can call those methods passing a value:

我们可以调用那些传递值的方法:

I mentioned “safe integer”. Also up above, with the MAX_SAFE_INTEGER and MIN_SAFE_INTEGER properties. What is a safe integer? It’s an integer that can be exactly represented as an IEEE-754 double precision number (all integers from (2^53 - 1) to -(2^53 - 1)). Out of this range, integers cannot be represented by JavaScript correctly. Out of the scope of the course, but here is a great explanation of that.

我提到了“安全整数”。 同样位于上方,具有MAX_SAFE_INTEGER和MIN_SAFE_INTEGER属性。 什么是安全整数? 它是一个整数,可以精确地表示为IEEE-754双精度数字(所有整数(从(2 ^ 53-1)到-(2 ^ 53-1)))。 超出此范围,JavaScript无法正确表示整数。 这超出了课程的范围,但这是对此的一个很好的解释

实例方法 (Instance methods)

When you use the new keyword to instantiate a value with the Number() function, we get a Number object in return:

当您使用new关键字通过Number()函数实例化一个值时,我们得到一个Number对象作为回报:

const age = new Number(36)
typeof age //object

This object offers a few unique methods you can use. Mostly to convert the number to specific formats.

该对象提供了一些可以使用的独特方法。 通常是将数字转换为特定格式。

  • .toExponential(): return a string representing the number in exponential notation

    .toExponential() :返回一个以指数表示形式表示数字的字符串

  • .toFixed(): return a string representing the number in fixed-point notation

    .toFixed() :返回一个字符串,该字符串以定点表示法表示数字

  • .toLocaleString(): return a string with the local specific conventions of the number

    .toLocaleString() :返回具有数字的本地特定约定的字符串

  • .toPrecision(): return a string representing the number to a specified precision

    .toPrecision() :返回表示数字的字符串,达到指定的精度

  • .toString(): return a string representing the specified object in the specified radix (base). Overrides the Object.prototype.toString() method

    .toString() :返回一个字符串,该字符串表示指定基数(基数)中的指定对象。 重写Object.prototype.toString()方法

  • .valueOf(): return the number primitive value of the object

    .valueOf() :返回对象的数字原始值

翻译自: https://flaviocopes.com/javascript-number/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值