Home  >  Article  >  Web Front-end  >  Detailed explanation of how JS converts scientific notation string into normal numerical format

Detailed explanation of how JS converts scientific notation string into normal numerical format

零到壹度
零到壹度Original
2018-03-21 16:17:246688browse

很多同学在使用Math类的一些相关方法或者原始大数据进行了计算的结果之后,经常会发现大数据都会被转换成科学计数法的格式(做财务数据方面的童鞋应该深有感触),而大多数情况,我们想要展现的只是普通数据形式,怎么给转换回来呢?

我们知道,导致这种情况出现的原因就是我们用Math类的一些方法处理了原始数据(比如Math.floor(),Math.abs()等),或者原始大数据进行了计算的结果,那么我们对其处理的位置就有了定义了:在所有数据处理(包括Math类方法处理和基本运算)完成后(保证数据转换后不再有相关计算改变格式)进行转换,来保证最终显示数据是我们转换后的数据。

上处理方法:

var num = 1545646435185456415646464545+1;        //1.5456464351854563e+27
num = (num-0).toLocaleString();                 //"1,545,646,435,185,460,000,000,000,000"    注意这样会丢失一些精度
num = num.toString().replace(/\$|\,/g, '');     //"1545646435185460000000000000"         将千分位形式的分隔符替换为空  最终显示结果

相关推荐:

java科学计数法转换为数字型字符串

The above is the detailed content of Detailed explanation of how JS converts scientific notation string into normal numerical format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn