如何格式化数字以始终在小数点前最多显示3位数字

时间:2019-02-19 02:22:00

标签: javascript math

我有一个jscalc.io计算器,它接受输入,并根据公式给出输出。 到目前为止,我已经将数字格式化为一个非常大的数字(因为我希望显示文本而不是eXX的功能(这是大约12个手动编写的输出),但是对于更高的数字(> e42,我需要eXX部分始终可整乘以3,基本上总是在小数点前显示最多3位数字。

这是我需要格式化的公式(10 * (Math.pow(inputs.mult,inputs.priv) -2)) input.mult本质上是一个常数1.5,其他数字是用户输入的

一些示例输出:

1.3844e43 --> 13.8440e42
5.9385e44 --> 593.8500e42
9.1234e45 --> stays the same (because 45 is divisible by 3)
2.9871e109 --> 29.8710e108

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

假设您可以将输入n解析为字符串,然后将其输出为字符串out,则可以处理字符串:

function toSpecialExponential(n){
    if(isNaN(n)) return NaN;

    //E.G. n = 1.3844e43;
    var splitN = parseFloat(n).toExponential().split("e"); //["1.3844","+43"]
    var frontN = parseFloat(splitN[0]); //1.3884
    var expo = parseInt(splitN[1]);   //43

    //Get modu = expo (mod 3)
    //Note that you cannot properly get the modulus of a negative number in JS, so we have to turn negative numbers positive as well...
    //Also indicator of how much to shift the decimal point by
    var modu = Math.abs(expo)%3; 

    //Special Case: expo is divisible by 3
    if(modu == 0) return parseFloat(n).toExponential().replace("+",""); //return n as is, parsed as an exponential but with the + signs removed. (1 => "1e+0")

    //Actual Parsing
    //Now with support for negative numbers
    var newExpo = (expo<0)?-((3-expo)-modu):expo-modu;
    var newFront = (expo<0)?frontN*Math.pow(10,3-modu):frontN*Math.pow(10,modu);

    //Hack to avoid floating point errors
    var _len = frontN.toString().replace(/([^0-9]+)/gi,"").length-Math.abs(expo-newExpo)-1;
    newFront = parseFloat(newFront).toFixed(_len);

    return newFront+"e"+newExpo;
}

编辑:

  • JS代码段
  • 小技巧可消除浮点错误。
  • 支持负数和指数

function toSpecialExponential(n){
    if(isNaN(n)) return NaN;

    //E.G. n = 1.3844e43;
    var splitN = parseFloat(n).toExponential().split("e"); //["1.3844","+43"]
   	var frontN = parseFloat(splitN[0]); //1.3884
   	var expo = parseInt(splitN[1]);   //43
    
    //Get modu = expo (mod 3)
    //Note that you cannot properly get the modulus of a negative number in JS, so we have to turn negative numbers positive as well...
    //Also indicator of how much to shift the decimal point by
    var modu = Math.abs(expo)%3; 
        
    //Special Case: expo is divisible by 3
    if(modu == 0) return parseFloat(n).toExponential().replace("+",""); //return n as is, parsed as an exponential but with the + signs removed. (1 => "1e+0")
    
    //Actual Parsing
    //Now with support for negative numbers
    var newExpo = (expo<0)?-((3-expo)-modu):expo-modu;
    var newFront = (expo<0)?frontN*Math.pow(10,3-modu):frontN*Math.pow(10,modu);
    
    //Hack to avoid floating point errors
    var _len = frontN.toString().replace(/([^0-9]+)/gi,"").length-Math.abs(expo-newExpo)-1;
    newFront = parseFloat(newFront).toFixed(_len);
    
    return newFront+"e"+newExpo;
}
<input type="text" id="input" value="1.3844e43" /> <input type="button" onclick="javascript:document.getElementById('out').innerHTML = toSpecialExponential(document.getElementById('input').value)" value="Get Special Exponential!">

<h3>Output</h3>
<div id="out"></div>