简体   繁体   中英

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:

6e-8

I don't want it to show 6e-8 , I want it to show 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. To print it "pretty", just write a function to convert it into 0.00000006 , eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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