Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
// NOTE: for the following, we explicitly avoid using stdlib packages in this particular package in order to avoid circular dependencies.
var abs = Math.abs; // eslint-disable-line stdlib/no-builtin-math
var lowercase = String.prototype.toLowerCase;
var uppercase = String.prototype.toUpperCase;
var replace = String.prototype.replace;
// VARIABLES //
var RE_EXP_POS_DIGITS = /e\+(\d)$/;
var RE_EXP_NEG_DIGITS = /e-(\d)$/;
var RE_ONLY_DIGITS = /^(\d+)$/;
var RE_DIGITS_BEFORE_EXP = /^(\d+)e/;
var RE_TRAILING_PERIOD_ZERO = /\.0$/;
var RE_PERIOD_ZERO_EXP = /\.0*e/;
var RE_ZERO_BEFORE_EXP = /(\..*[^0])0*e/;
// MAIN //
/**
* Formats a token object argument as a floating-point number.
*
* @private
* @param {number} f - parsed number
* @param {Object} token - token object
* @throws {Error} must provide a valid floating-point number
* @returns {string} formatted token argument
*/
function formatDouble( f, token ) {
var digits;
var out;
switch ( token.specifier ) {
case 'e':
case 'E':
out = f.toExponential( token.precision );
break;
case 'f':
case 'F':
out = f.toFixed( token.precision );
break;
case 'g':
case 'G':
if ( abs( f ) < 0.0001 ) {
digits = token.precision;
if ( digits > 0 ) {
digits -= 1;
}
out = f.toExponential( digits );
} else {
out = f.toPrecision( token.precision );
}
if ( !token.alternate ) {
out = replace.call( out, RE_ZERO_BEFORE_EXP, '$1e' );
out = replace.call( out, RE_PERIOD_ZERO_EXP, 'e' );
out = replace.call( out, RE_TRAILING_PERIOD_ZERO, '' );
}
break;
default:
throw new Error( 'invalid double notation. Value: ' + token.specifier );
}
out = replace.call( out, RE_EXP_POS_DIGITS, 'e+0$1' );
out = replace.call( out, RE_EXP_NEG_DIGITS, 'e-0$1' );
if ( token.alternate ) {
out = replace.call( out, RE_ONLY_DIGITS, '$1.' );
out = replace.call( out, RE_DIGITS_BEFORE_EXP, '$1.e' );
}
if ( f >= 0 && token.sign ) {
out = token.sign + out;
}
out = ( token.specifier === uppercase.call( token.specifier ) ) ?
uppercase.call( out ) :
lowercase.call( out );
return out;
}
// EXPORTS //
module.exports = formatDouble;
|