source: [view]
var f = parseFloat(token.arg);
if(!isFinite(f)){ // isNaN(f) || f == Number.POSITIVE_INFINITY || f == Number.NEGATIVE_INFINITY)
// allow this only if arg is number
if(typeof token.arg != "number"){
throw new Error("format argument '" + token.arg + "' not a float; parseFloat returned " + f);
}
// C99 says that for 'f':
// infinity -> '[-]inf' or '[-]infinity' ('[-]INF' or '[-]INFINITY' for 'F')
// NaN -> a string starting with 'nan' ('NAN' for 'F')
// this is not commonly implemented though.
//return '' + f;
f = 0;
}
switch(token.doubleNotation) {
case 'e': {
token.arg = f.toExponential(token.precision);
break;
}
case 'f': {
token.arg = f.toFixed(token.precision);
break;
}
case 'g': {
// C says use 'e' notation if exponent is < -4 or is >= prec
// ECMAScript for toPrecision says use exponential notation if exponent is >= prec,
// though step 17 of toPrecision indicates a test for < -6 to force exponential.
if(Math.abs(f) < 0.0001){
//print("forcing exponential notation for f=" + f);
token.arg = f.toExponential(token.precision > 0 ? token.precision - 1 : token.precision);
}else{
token.arg = f.toPrecision(token.precision);
}
// In C, unlike 'f', 'gG' removes trailing 0s from fractional part, unless alternative format flag ("#").
// But ECMAScript formats toPrecision as 0.00100000. So remove trailing 0s.
if(!token.alternative){
//print("replacing trailing 0 in '" + s + "'");
token.arg = token.arg.replace(/(\..*[^0])0*/, "$1");
// if fractional part is entirely 0, remove it and decimal point
token.arg = token.arg.replace(/\.0*e/, 'e').replace(/\.0$/,'');
}
break;
}
default: throw new Error("unexpected double notation '" + token.doubleNotation + "'");
}
// C says that exponent must have at least two digits.
// But ECMAScript does not; toExponential results in things like "1.000000e-8" and "1.000000e+8".
// Note that s.replace(/e([\+\-])(\d)/, "e$10$2") won't work because of the "$10" instead of "$1".
// And replace(re, func) isn't supported on IE50 or Safari1.
token.arg = token.arg.replace(/e\+(\d)$/, "e+0$1").replace(/e\-(\d)$/, "e-0$1");
// Ensure a '0' before the period.
// Opera implements (0.001).toString() as '0.001', but (0.001).toFixed(1) is '.001'
if(dojo.isOpera){
token.arg = token.arg.replace(/^\./, '0.');
}
// if alt, ensure a decimal point
if(token.alternative){
token.arg = token.arg.replace(/^(\d+)$/,"$1.");
token.arg = token.arg.replace(/^(\d+)e/,"$1.e");
}
if(f >= 0 && token.sign){
token.arg = token.sign + token.arg;
}
token.arg = token.toUpper ? token.arg.toUpperCase() : token.arg.toLowerCase();