[英]I came up with a way to format numbers to scientific notation, how can I call this function whenever I need it?
I just started learning js and so far am making good progress.我刚开始学习 js,到目前为止进展顺利。 I have encountered an issue tho, and the title should basically tell you everything you need to know, but here goes anyways.我遇到了一个问题,标题基本上应该告诉你你需要知道的一切,但不管怎样,还是来了。
For the project I want to realize eventually, I'll need rather big numbers.对于我最终想要实现的项目,我需要相当大的数字。 So I came up with the following to format them to scientific notation:所以我想出了以下方法将它们格式化为科学记数法:
<script>
var rawNumber = 172 ** 25; // The number that shall be formatted into scientific notation
var exponentDisplay = "e+"; // Self explainatory
var numberRelevantDigits = 0; // The digits displayed before the "e+"
var numberExponent = 0; // Self explainatory
var formattedNumberDisplay = 0; // What will be displayed
function formatNumber() { // The function triggered by clicking the button
var numberExponent = Math.floor( Math.log(rawNumber) / Math.log(10) ); // Calculating the exponent of the number in scientific notation
var numberRelevantDigits = rawNumber / 10 ** numberExponent; // Dividing the number by 10 to the power of its exponent
var numberRelevantDigits = numberRelevantDigits.toFixed(3); // Rounds the relevant digits to 3 decimals
var formattedNumberDisplay = numberRelevantDigits + exponentDisplay + numberExponent; // Adding the relevant digits, "e+" and the exponent into a string
document.getElementById("formattedNumberDisplay").innerHTML = formattedNumberDisplay; // Changing the display to the formatted number
}
</script>
This works as intended, but I don't want to put this chunk of code whenever I need a number formatted.这按预期工作,但我不想在需要格式化数字时放置这段代码。 Is there a way (or rather: which is the way) to make it so I can just call that function whenever I need a number formatted?有没有一种方法(或者更确切地说:哪种方法)可以做到这一点,这样我就可以在需要格式化数字时调用它 function? Even if I need use different variables?即使我需要使用不同的变量?
Let me apologize again, since this has likely been answered a dozen times before, but I don't even have a clue what to even look for.让我再次道歉,因为这可能已经回答了十几次,但我什至不知道要寻找什么。
Thank y'all in advance.提前谢谢大家。
Your file NumberFormater.js will look like:您的文件 NumberFormater.js 将如下所示:
export formatNumber = (
rawNumber,
exponentDisplay,
numberRelevantDigits,
numberExponent,
formattedNumberDisplay
) => {
let numberExponent = Math.floor( Math.log(rawNumber) / Math.log(10) ); // Calculating the exponent of the number in scientific notation
let numberRelevantDigits = rawNumber / 10 ** numberExponent; // Dividing the number by 10 to the power of its exponent
let numberRelevantDigits = numberRelevantDigits.toFixed(3); // Rounds the relevant digits to 3 decimals
let formattedNumberDisplay = numberRelevantDigits + exponentDisplay + numberExponent; // Adding the relevant digits, "e+" and the exponent into a string
document.getElementById("formattedNumberDisplay").innerHTML = formattedNumberDisplay; // Changing the display to the formatted number
}
then when needed:然后在需要时:
import { formatNumber } from './NumberFormater.js';
const rawNumber = 172 ** 25; // The number that shall be formatted into scientific notation
const exponentDisplay = "e+"; // Self explainatory
const numberRelevantDigits = 0; // The digits displayed before the "e+"
const numberExponent = 0; // Self explainatory
const formattedNumberDisplay = 0; // What will be displayed
formatNumber( rawNumber, exponentDisplay, numberRelevantDigits, numberExponent, formattedNumberDisplay);
read more on js modules https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules Hope I've helped.阅读有关 js 模块的更多信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules希望我有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.