Opposite of Number.toExponential in JS(JS中Number.toExponential的对面)
问题描述
我需要在 JavaScript 中以非指数形式获取一个极大数的值.Number.toFixed
只是将它以指数形式作为字符串返回,这比我的差.
I need to get the value of an extremely large number in JavaScript in non-exponential form. Number.toFixed
simply returns it in exponential form as a string, which is worse than what I had.
这是 Number.toFixed
返回的内容:
>>> x = 1e+31
1e+31
>>> x.toFixed()
"1e+31"
Number.toPrecision
也不起作用:
>>> x = 1e+31
1e+31
>>> x.toPrecision( 21 )
"9.99999999999999963590e+30"
我想要的是:
>>> x = 1e+31
1e+31
>>> x.toNotExponential()
"10000000000000000000000000000000"
我可以编写自己的解析器,但如果存在,我宁愿使用原生 JS 方法.
I could write my own parser but I would rather use a native JS method if one exists.
推荐答案
答案是没有这样的内置函数.我搜索了高低.这是我用来将数字拆分为符号、系数(小数点前的数字)、小数部分(小数点后的数字)和指数的 RegExp:
The answer is there's no such built-in function. I've searched high and low. Here's the RegExp I use to split the number into sign, coefficient (digits before decimal point), fractional part (digits after decimal point) and exponent:
/^([+-])?(d+).?(d*)[eE]([+-]?d+)$/
自己动手"就是答案,您已经这样做了.
"Roll your own" is the answer, which you already did.
这篇关于JS中Number.toExponential的对面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JS中Number.toExponential的对面


基础教程推荐
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01