printj/bits/70_float.js
SheetJS ed55a202b3 version bump 1.0.1: typings
- typescript definitions and types
- flowtype definition refinement
2017-07-04 02:26:51 -04:00

139 lines
4.5 KiB
JavaScript

Vnum = Number(arg);
if(arg === null) Vnum = 0/0;
if(len == "L") bytes = 12;
var isf/*:boolean*/ = isFinite(Vnum);
if(!isf) { /* Infinity or NaN */
if(Vnum < 0) O = "-";
else if(flags.indexOf("+") > -1) O = "+";
else if(flags.indexOf(" ") > -1) O = " ";
O += (isnan(Vnum)) ? "nan" : "inf";
} else {
var E/*:number*/ = 0;
if(prec == -1 && isnum != 4) prec = 6;
/* g/G conditional behavior */
if(isnum == 3) {
O = Vnum.toExponential(1);
E = +O.substr(O.indexOf("e") + 1);
if(prec === 0) prec = 1;
if(prec > E && E >= -4) { NUM(11); prec = prec -(E + 1); }
else { NUM(12); prec = prec - 1; }
}
/* sign: workaround for negative zero */
var sg/*:string*/ = (Vnum < 0 || 1/Vnum == -Infinity) ? "-" : "";
if(Vnum < 0) Vnum = -Vnum;
switch(isnum) {
/* f/F standard */
case 1: case 11:
if(Vnum < 1e21) {
O = Vnum.toFixed(prec);
if(isnum == 1) { if(prec===0 &&alt&& O.indexOf(".")==-1) O+="."; }
else if(!alt) O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");
else if(O.indexOf(".") == -1) O+= ".";
break;
}
O = Vnum.toExponential(20);
E = +O.substr(O.indexOf("e")+1);
O = O.charAt(0) + O.substr(2,O.indexOf("e")-2);
O = O + PADS(E - O.length + 1, "0");
if(alt || (prec > 0 && isnum !== 11)) O = O + "." + PADS(prec, "0");
break;
/* e/E exponential */
case 2: case 12:
O = Vnum.toExponential(prec);
E = O.indexOf("e");
if(O.length - E === 3) O = O.substr(0, E+2) + "0" + O.substr(E+2);
if(alt && O.indexOf(".") == -1) O = O.substr(0,E) +"."+ O.substr(E);
else if(!alt && isnum == 12) O = O.replace(/\.0*e/, "e").replace(/\.(\d*[1-9])0*e/, ".$1e");
break;
/* a/A hex */
case 4:
if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+PADS(prec,"0"):"")+"p+0"; break;}
O = Vnum.toString(16);
/* First char 0-9 */
var ac/*:number*/ = O.charCodeAt(0);
if(ac == 48) {
ac = 2; E = -4; Vnum *= 16;
while(O.charCodeAt(ac++) == 48) { E -= 4; Vnum *= 16; }
O = Vnum.toString(16);
ac = O.charCodeAt(0);
}
var ai/*:number*/ = O.indexOf(".");
if(O.indexOf("(") > -1) {
/* IE exponential form */
var am/*:?Array<any>*/ = O.match(/\(e(.*)\)/);
var ae/*:number*/ = am ? (+am[1]) : 0;
E += 4 * ae; Vnum /= Math.pow(16, ae);
} else if(ai > 1) {
E += 4 * (ai - 1); Vnum /= Math.pow(16, ai - 1);
} else if(ai == -1) {
E += 4 * (O.length - 1); Vnum /= Math.pow(16, O.length - 1);
}
/* at this point 1 <= Vnum < 16 */
#ifndef DO_NOT_NORMALIZE
if(bytes > 8) {
if(ac < 50) { E -= 3; Vnum *= 8; }
else if(ac < 52) { E -= 2; Vnum *= 4; }
else if(ac < 56) { E -= 1; Vnum *= 2; }
/* at this point 8 <= Vnum < 16 */
} else {
if(ac >= 56) { E += 3; Vnum /= 8; }
else if(ac >= 52) { E += 2; Vnum /= 4; }
else if(ac >= 50) { E += 1; Vnum /= 2; }
/* at this point 1 <= Vnum < 2 */
}
#endif
O = Vnum.toString(16);
if(O.length > 1) {
if(O.length > prec+2 && O.charCodeAt(prec+2) >= 56) {
var _f/*:boolean*/ = O.charCodeAt(0) == 102;
O = (Vnum + 8 * Math.pow(16, -prec-1)).toString(16);
if(_f && O.charCodeAt(0) == 49) E += 4;
}
if(prec > 0) {
O = O.substr(0, prec + 2);
if(O.length < prec + 2) {
if(O.charCodeAt(0) < 48) O = O.charAt(0) + PADS((prec + 2 - O.length), "0") + O.substr(1);
else O += PADS((prec + 2 - O.length), "0");
}
} else if(prec === 0) O = O.charAt(0) + (alt ? "." : "");
} else if(prec > 0) O = O + "." + PADS(prec,"0");
else if(alt) O = O + ".";
O = "0x" + O + "p" + (E>=0 ? "+" + E : E);
break;
}
if(sg === "") {
if(flags.indexOf("+") > -1) sg = "+";
else if(flags.indexOf(" ") > -1) sg = " ";
}
O = sg + O;
}
/* width */
if(width > O.length) {
if(flags.indexOf("-") > -1) {
O = O + PADS((width - O.length), " ");
} else if(flags.indexOf("0") > -1 && O.length > 0 && isf) {
pad = PADS((width - O.length), "0");
if(O.charCodeAt(0) < 48) {
if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3);
else O = O.substr(0,1) + pad + O.substring(1);
}
else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2);
else O = pad + O;
} else {
O = PADS((width - O.length), " ") + O;
}
}
if(c < 96) O = O.toUpperCase();