简体   繁体   English

如何防止极小的数字转换为科学计数法?

[英]How to prevent very small numbers from being converted to scientific notation?

I have to use very small numbers: 我必须使用非常小的数字:

var x = 0.00000006;

When I run console.log(x) , it shows: 当我运行console.log(x)时 ,它显示:

6e-8

I don't want it to show 6e-8 , I want it to show 0.00000006 . 我不希望它显示6e-8 ,我希望它显示0.00000006

Later I need to plot it on a graph, so I can't convert it to a string. 稍后我需要将其绘制在图形上,因此无法将其转换为字符串。 How to keep it a small number without converting it to a string or scientific notation? 如何在不将其转换为字符串或科学计数法的情况下将其保留为较小的数字?

You can leave your number as it is. 您可以保持原样。 The 6e-8 is just the way your console represents this small number. 6e-8只是您的控制台代表这个小数字的方式。 To print it "pretty", just write a function to convert it into 0.00000006 , eg console.log(prettyfy(x)) . 要打印“漂亮”,只需编写一个函数将其转换为0.00000006 ,例如console.log(prettyfy(x)) This way, you leave your number as it is and can print any number in your desired format. 这样,您就可以保留您的电话号码,并可以按所需格式打印任何电话号码。

You can convert it to "fixed" shape and get it to look like you want. 您可以将其转换为“固定”形状,并使其看起来像您想要的。 Example would be: 例如:

var number = 6e-8; //this is your number
number = number.toFixed(8) //but since you won't always know how many decimal points you have you can use something like
number = number.toFixed(number.toString().split('-')[1]); //where you split your number, see how many decimals it has and pass that number to .toFixed method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM