How to convert a String containing Scientific Notation to correct Javascript number format(如何将包含科学记数法的字符串转换为正确的 Javascript 数字格式)

本文介绍了如何将包含科学记数法的字符串转换为正确的 Javascript 数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串例如: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.

推荐答案

将科学计数法中的数字转换为字符串:

另一个问题

ConvertingaNumberinScientificNotationtoaString:

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 数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!