@@ -2147,51 +2147,6 @@ const FormatList = [
21472147 "Ce" ,
21482148] ;
21492149
2150- // Bad browsers (like Safari) only recently implemented this.
2151- const supportsFormatToParts = typeof Intl . NumberFormat . prototype . formatToParts === 'function'
2152-
2153- // In some browsers, this will return an empty-1 length array (?), causing a "TypeError: Cannot read property 'value' of undefined"
2154- // if we destructure it... To reproduce: ` const [ { value } ] = []; `
2155- // https://discord.com/channels/677271830838640680/730669616870981674/830218436201283584
2156- const IntlFormatter = ! supportsFormatToParts
2157- ? null
2158- : Intl . NumberFormat ( )
2159- . formatToParts ( 1000.1 )
2160- . filter ( ( part ) => part . type === 'decimal' || part . type === 'group' )
2161-
2162- // gets the system number delimiter and decimal values, defaults to en-US
2163- const [ { value : group } , { value : dec } ] = IntlFormatter ?. length !== 2
2164- ? [ { value : ',' } , { value : '.' } ]
2165- : IntlFormatter
2166-
2167- // Number.toLocaleString opts for 2 decimal places
2168- const locOpts = { minimumFractionDigits : 2 , maximumFractionDigits : 2 }
2169-
2170- const padEvery = ( str : string , places = 3 ) => {
2171- let step = 1
2172- let newStr = ''
2173- const strParts = str . split ( '.' )
2174- // don't take any decimal places
2175- for ( let i = strParts [ 0 ] . length - 1 ; i >= 0 ; i -- ) {
2176- // pad every [places] places if we aren't at the beginning of the string
2177- if ( step ++ === places && i !== 0 ) {
2178- step = 1
2179- newStr = group + str [ i ] + newStr
2180- } else {
2181- newStr = str [ i ] + newStr
2182- }
2183- }
2184- // re-add decimal places
2185- if ( strParts [ 1 ] !== undefined ) {
2186- newStr += dec + strParts [ 1 ]
2187- } // see https://www.npmjs.com/package/flatstr
2188- /* eslint-disable */
2189-
2190- ; ( newStr as unknown as number ) | 0
2191- /* eslint-enable */
2192- return newStr
2193- }
2194-
21952150/**
21962151 * This function displays the numbers such as 1,234 or 1.00e1234 or 1.00e1.234M.
21972152 * @param input value to format
@@ -2216,30 +2171,12 @@ export const format = (
22162171 return '0 [null]'
22172172 }
22182173
2219- // NaN check
2220- if ( input !== input ) {
2174+ if ( Number . isNaN ( input ) ) {
22212175 return '0 [NaN]'
22222176 }
22232177
22242178 const inputType = typeof input
22252179
2226- if (
2227- // this case handles numbers less than 1e-6 and greater than 0
2228- inputType === 'number'
2229- && player . notation === 'Default'
2230- && ( input as number ) < ( ! fractional ? 1e-3 : 1e-15 ) // arbitrary number, don't change 1e-3
2231- && ( input as number ) > 0 // don't handle negative numbers, probably could be removed
2232- ) {
2233- return input . toExponential ( accuracy )
2234- } else if (
2235- inputType === 'number'
2236- && player . notation === 'Default'
2237- && - ( input as number ) < ( ! fractional ? 1e-3 : 1e-15 ) // arbitrary number, don't change 1e-3
2238- && - ( input as number ) > 0
2239- ) {
2240- return `-${ ( - ( input as number ) ) . toExponential ( accuracy ) } `
2241- }
2242-
22432180 let power ! : number
22442181 let mantissa ! : number
22452182 if ( inputType === 'number' ) {
@@ -2251,7 +2188,7 @@ export const format = (
22512188 }
22522189
22532190 // Gets power and mantissa if input is of type number and isn't 0
2254- power = Math . floor ( Math . log10 ( Math . abs ( input as number ) ) )
2191+ power = Math . floor ( Math . log10 ( input as number ) )
22552192 mantissa = ( input as number ) / Math . pow ( 10 , power )
22562193 } else if ( input instanceof Decimal ) {
22572194 if ( input . lessThan ( 0 ) ) {
@@ -2280,40 +2217,24 @@ export const format = (
22802217 if ( power < - 100 ) {
22812218 return '0'
22822219 }
2283- if ( player . notation === 'Pure Engineering' ) {
2284- const powerOver = power % 3 < 0 ? 3 + ( power % 3 ) : power % 3
2285- power = power - powerOver
2286- mantissa = mantissa * Math . pow ( 10 , powerOver )
2287- } else if ( player . notation === 'Pure Scientific' ) {
2288- if ( power >= 1e6 ) {
2289- if ( ! Number . isFinite ( power ) ) {
2290- return 'Infinity'
2291- }
2292- return `E${ format ( power , 3 ) } `
2220+
2221+ if ( ! Number . isFinite ( power ) ) {
2222+ return 'Infinity'
2223+ }
2224+
2225+ // Make the above code apply to Default notation as well
2226+ // Also make the block below apply to Pure Scientific and Pure Engineering notations || rus9384
2227+ // This case handles numbers less than 1e-3 and greater than -1e-3
2228+ if ( inputType === 'number' && Math . abs ( input as number ) < ( ! fractional ? 1e-3 : 1e-15 ) ) { // Arbitrary number, don't change 1e-3
2229+ const formatOpts = {
2230+ minimumSignificantDigits : 1 + accuracy ,
2231+ maximumSignificantDigits : 1 + accuracy ,
2232+ roundingMode : 'trunc' as const ,
2233+ notation : player . notation === 'Pure Engineering' ? 'engineering' as const : 'scientific' as const
22932234 }
2294- accuracy = power === 2 && accuracy > 2 ? 2 : accuracy
2295- if ( power >= 6 || power < 0 ) {
2296- accuracy = accuracy < 2 ? 2 : accuracy
2297- // Makes the power group 3 with commas
2298- const mantissaLook = (
2299- Math . floor ( mantissa * Math . pow ( 10 , accuracy ) ) / Math . pow ( 10 , accuracy )
2300- ) . toLocaleString ( undefined , locOpts )
2301- const powerLook = padEvery ( power . toString ( ) )
2302- // returns format (1.23e456,789)
2303- return `${ mantissaLook } e${ powerLook } `
2304- }
2305- mantissa = mantissa * Math . pow ( 10 , power )
2306- if ( mantissa - Math . floor ( mantissa ) > 0.9999999 ) {
2307- mantissa = Math . ceil ( mantissa )
2308- }
2309-
2310- return (
2311- Math . floor ( mantissa * Math . pow ( 10 , accuracy ) ) / Math . pow ( 10 , accuracy )
2312- ) . toLocaleString ( undefined , {
2313- minimumFractionDigits : accuracy ,
2314- maximumFractionDigits : accuracy
2315- } )
2235+ return input . toLocaleString ( undefined , formatOpts )
23162236 }
2237+
23172238 // If the power is negative, then we will want to address that separately.
23182239 if ( power < 0 && inputType === 'number' && fractional ) {
23192240 if ( power <= - 15 ) {
@@ -2362,48 +2283,67 @@ export const format = (
23622283 // Gets the standard representation of the number, safe as power is guaranteed to be > -12 and < 12
23632284 let standard = mantissa * Math . pow ( 10 , power )
23642285 let standardString : string
2286+ let digits = accuracy
2287+ if ( power >= 2 + ( long ? 1 : 0 ) ) {
2288+ digits = 0
2289+ } else if ( power === 2 && accuracy > 2 ) {
2290+ digits = 2
2291+ }
2292+
2293+ Math . max ( 0 , Math . min ( accuracy , accuracy + 2 - power ) )
23652294 // Rounds up if the number experiences a rounding error
23662295 if ( standard - Math . floor ( standard ) > 0.9999999 ) {
23672296 standard = Math . ceil ( standard )
23682297 }
2369- // If the power is less than 1 or format long and less than 3 apply toFixed(accuracy) to get decimal places
2370- if ( ( power < 2 || ( long && power < 3 ) ) && accuracy > 0 ) {
2371- standardString = standard . toFixed (
2372- power === 2 && accuracy > 2 ? 2 : accuracy
2373- )
2374- } else {
2375- // If it doesn't fit those criteria drop the decimal places
2376- standard = Math . floor ( standard )
2377- standardString = standard . toString ( )
2298+
2299+ const formatOpts = {
2300+ minimumFractionDigits : digits ,
2301+ maximumFractionDigits : digits ,
2302+ roundingMode : 'trunc' as const
23782303 }
2304+ standardString = standard . toLocaleString ( undefined , formatOpts )
2305+ //return padEvery(standardString) // Split it on the decimal place
2306+ return standardString
2307+ }
23792308
2380- // Split it on the decimal place
2381- return padEvery ( standardString )
2382- } else if ( power < 1e6 ) {
2383- // If the power is less than 1e6 then apply standard scientific notation
2384- // Makes mantissa be rounded down to 2 decimal places
2385- const mantissaLook = ( Math . floor ( mantissa * 100 ) / 100 ) . toLocaleString (
2386- undefined ,
2387- locOpts
2388- )
2389- // Makes the power group 3 with commas
2390- const powerLook = padEvery ( power . toString ( ) )
2391- // returns format (1.23e456,789)
2392- return `${ mantissaLook } e${ powerLook } `
2309+ accuracy = Math . max ( 3 , accuracy + 1 )
2310+ // Only apply Engineering notation if we can see all digits of the exponent || rus9384
2311+ if ( player . notation === 'Pure Engineering' && ( power < 1e6 || longExponent && power < 1e12 ) ) {
2312+ const powerOver = ( power % 3 + 3 ) % 3
2313+ power -= powerOver
2314+ mantissa *= Math . pow ( 10 , powerOver )
2315+ }
2316+
2317+ // Number.toLocaleString opts for 'accuracy' decimal places
2318+ const locOpts = {
2319+ minimumSignificantDigits : accuracy ,
2320+ maximumSignificantDigits : accuracy ,
2321+ roundingMode : 'trunc' as const
2322+ }
2323+
2324+ if ( power < 1e6 ) {
2325+ // If the power is less than 1e6 then apply standard scientific/engineering notation
2326+ // Makes mantissa be rounded down to 'accuracy' decimal places
2327+ const mantissaLook = mantissa . toLocaleString ( undefined , locOpts )
2328+ //const powerLook = padEvery(power.toString()) // Makes the power group 3 with commas
2329+ const powerLook = power . toLocaleString ( )
2330+ return `${ mantissaLook } e${ powerLook } ` // Returns format (1.23e456,789)
23932331 } else if ( power >= 1e6 ) {
2394- if ( ! Number . isFinite ( power ) ) {
2395- return 'Infinity'
2332+ // The only difference between Default and Pure Scientific is how it handles numbers larger than 1e1M / E1e6 || rus9384
2333+ if ( player . notation === 'Pure Scientific' ) {
2334+ return `E${ format ( power , 3 ) } `
23962335 }
23972336
23982337 if ( longExponent ) {
2399- const mantissaLook = ( Math . floor ( mantissa * 100 ) / 100 ) . toLocaleString ( undefined , locOpts )
2338+ const mantissaLook = mantissa . toLocaleString ( undefined , locOpts )
24002339 return `${ mantissaLook } e${ format ( power , 0 , true ) } `
24012340 }
2341+
24022342 // if the power is greater than 1e6 apply notation scientific notation
24032343 // Makes mantissa be rounded down to 2 decimal places
24042344 const mantissaLook = testing && truncate
24052345 ? ''
2406- : ( Math . floor ( mantissa * 100 ) / 100 ) . toLocaleString ( undefined , locOpts )
2346+ : mantissa . toLocaleString ( undefined , locOpts )
24072347
24082348 // Drops the power down to 4 digits total but never greater than 1000 in increments that equate to notations, (1234000 -> 1.234) ( 12340000 -> 12.34) (123400000 -> 123.4) (1234000000 -> 1.234)
24092349 const powerDigits = Math . ceil ( Math . log10 ( power ) )
0 commit comments