简体   繁体   English

我想出了一种将数字格式化为科学计数法的方法,我如何在需要时调用这个 function?

[英]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.提前谢谢大家。

  • Create a js file and import it whenever you want创建一个js文件并在需要时导入它
  • also you should pass the variables to the functions instead of declaring them as a var您还应该将变量传递给函数,而不是将它们声明为 var
  • inside funciton use const and let, you might some ugly bugs in the future if you don't.在函数内部使用 const 和 let,如果不这样做,将来可能会出现一些丑陋的错误。

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.

相关问题 如何将数字转换为科学记数法? - How can I convert numbers into scientific notation? 如何将 JavaScript BigInt 值转换为科学记数法? - How can I convert a JavaScript BigInt value to Scientific Notation? 如何以特定方式格式化数字? - How can I format numbers in a specific way? 如何用科学计数法格式化数字以在Javascript中显示更少的数字? - How do I format a number in scientific notation to show less digits in Javascript? 我想使用 exec() 函数在我的 php 文件中运行 javascript 文件,但它出现了错误消息,我该如何解决? - i want to run javascript file in my php file with exec() function, but it came up with error message , how can i fix it? 在Ember中,每当属性更改时,如何在控制器中调用函数 - In Ember How Should I Call a Function in the Controller Whenever A Property Changes 每当我触摸时,都会使函数调用onclick - Making a function call onclick whenever I touch AngularJs:我有一个选择下拉列表,它在更改时仅调用一次函数。 每当更改时如何使它调用函数? - AngularJs: I have the select droplist that calls a function only once when changed. How can I make it call the function whenever it is changed? 有没有办法截断 Javascript 中的科学记数法数字? - Is there a way to truncate scientific notation numbers in Javascript? 如何使用toLocaleString格式化大数字? - How can I format big numbers with toLocaleString?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM