问题描述
我有一个字符串例如: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
将科学计数法中的数字转换为字符串:
另一个问题
Converting a Number in Scientific Notation to a String:
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设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)