简体   繁体   中英

How to format a number to always show up to 3 digits before decimal

I have a jscalc.io calculator that takes an input and according to a formula it gives me an output.

So far I've formatted the numbers up to a really big number (because I wanted text shown instead of the eXX power (that's about 12 manually written outputs), but for higher numbers (>e42 I need the eXX part to be always divisible by 3, basically always show up to 3 digits before the decimal).

Here's my formula (10 * (Math.pow(inputs.mult,inputs.priv) -2)) that I need formatted

inputs.mult is essentially a constant of 1.5, the other number is user entered

Some example outputs:

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

Assuming you are fine with parsing your input n to string, and outputting it as string out , it becomes string processing:

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;
}

EDITS:

  • JS Snippet
  • Small hack to get rid of floating point errors.
  • Support for negative numbers and exponents

 function toSpecialExponential(n){ if(isNaN(n)) return NaN; //EG 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> 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM