我有一个字符串例如:4.874915326E7".将其转换为 javascript 数字格式的最佳方法是什么?(整数或浮点数)?如果我尝试 parseInt(),最后的 E 将被忽略.
I have a String e.g: "4.874915326E7". What is the best way to convert it to a javascript number format? (int or float)? if I try parseInt(), the E at the end is ignored.
这个答案似乎引起了一些混乱.最初的问题是询问如何将字符串形式的科学记数法转换为数字(以便可以用于计算).但是,找到此答案的大量人似乎认为这是关于将 javascript 表示为科学记数法的数字转换为更美观的格式.如果这实际上是您的目标(演示),那么您应该将数字转换为字符串.请注意,这意味着您将无法在计算中轻松使用它.
将其作为字符串传递给 Number 函数.
Pass it as a string to the Number function.
Number("4.874915326E7") // returns 48749153.26
Number("4E27") // returns 4e+27
另一个问题
This is best answered by another question, but from that question I personally like the solution that uses .toLocaleString(). Note that that particular solution doesn't work for negative numbers. For your convenience, here is an example:
(4e+27).toLocaleString('fullwide', {useGrouping:false}) // returns "4000000000000000000000000000"
这篇关于如何将包含科学记数法的字符串转换为正确的 Javascript 数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)