I just started learning js and so far am making good progress. 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? 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:
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.
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.