我必须使用非常小的数字:
var x = 0.00000006;
当我运行console.log(x)时,它会显示:
6e-8
我不希望它显示6e-8,而是想显示0.00000006。
后来我需要将其绘制在图表上,因此不能将其转换为字符串。如何保持一个小数而不将它转换为字符串或科学计数法?
我必须使用非常小的数字:
var x = 0.00000006;
6e-8
我不希望它显示6e-8,而是想显示0.00000006。
后来我需要将其绘制在图表上,因此不能将其转换为字符串。如何保持一个小数而不将它转换为字符串或科学计数法?
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
这将更加正确,仅在e-符号后的最后一部分不足以始终提供您需要在toFixed中提供的数字。
// containing Scientific Notation to a readable number string
export const convertScientificNotationNumber = (value) => {
const decimalsPart = value?.toString()?.split('.')?.[1] || '';
const eDecimals = Number(decimalsPart?.split('e-')?.[1]) || 0;
const countOfDecimals = decimalsPart.length + eDecimals;
return Number(value).toFixed(countOfDecimals);
//0.4210854715202004e-14).toFixed(30)
};