大家看看我是不是见鬼了???

2017-07-19 17:36:53 +08:00
 XiaoFaye

5782 次点击
所在节点    程序员
24 条回复
Sirormy
2017-07-20 11:00:08 +08:00
还真没注意过,Mozilla 的文档写的。

“ A string representing the given number using fixed-point notation.”

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
caniuse
2017-07-20 12:32:51 +08:00
toFixed() 方法使用定点表示法来格式化一个数。

语法
numObj.toFixed(digits)
参数

digits
小数点后数字的个数;介于 0 到 20 (包括)之间,实现环境可能支持更大范围。如果忽略该参数,则默认为 0。
返回值

一个数值的字符串表现形式,不使用指数记数法,而是在小数点后有 digits 位数字。该数值在必要时进行四舍五入,另外在必要时会用 0 来填充小数部分,以便小数部分有指定的位数。 如果数值大于 1e+21,该方法会简单调用 Number.prototype.toString()并返回一个指数记数法格式的字符串。

抛出异常

RangeError
如果 digits 参数太小或太大。0 到 20 (包括)之间的值不会引起 RangeError。实现环境( implementations )也可以支持更大或更小的值。
TypeError
如果该方法在一个非 Number 类型的对象上调用。
示例
var numObj = 12345.6789;

numObj.toFixed(); // 返回 "12346":进行四舍五入,不包括小数部分
numObj.toFixed(1); // 返回 "12345.7":进行四舍五入

numObj.toFixed(6); // 返回 "12345.678900":用 0 填充

(1.23e+20).toFixed(2); // 返回 "123000000000000000000.00"

(1.23e-10).toFixed(2); // 返回 "0.00"

2.34.toFixed(1); // 返回 "2.3"

-2.34.toFixed(1); // 返回 -2.3 (由于操作符优先级,负数不会返回字符串)

(-2.34).toFixed(1); // 返回 "-2.3" (若用括号提高优先级,则返回字符串)
规范
Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.5.
ECMAScript 5.1 (ECMA-262)
Number.prototype.toFixed Standard
ECMAScript 2015 (6th Edition, ECMA-262)
Number.prototype.toFixed Standard
浏览器兼容性
Desktop Mobile
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
相关链接
Number.prototype.toExponential()
Number.prototype.toPrecision()
Number.prototype.toString()
AJian
2017-07-20 18:38:44 +08:00
你可以,+a>+b
AJian
2017-07-20 18:40:30 +08:00
归根结底就是用的字符串做比较,字符串比较的是 ASCII 码

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/376466

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX