@@ -2066,11 +2066,9 @@ function num2frac(num) {
20662066var SIPREFIXES = [ 'f' , 'p' , 'n' , 'μ' , 'm' , '' , 'k' , 'M' , 'G' , 'T' ] ;
20672067
20682068// extending SI prefixes
2069- var SIPREFIXES_EXTENDED = [ 'q' , 'r' , 'y' , 'z' , 'a' , 'f' , 'p' , 'n' , 'μ' , 'm' , '' , 'k' , 'M' , 'G' , 'T' , 'P' , 'E' , 'Z' , 'Y' , 'R' , 'Q' ] ;
2069+ var SIPREFIXES_EXTENDED = [ 'q' , 'r' , 'y' , 'z' , 'a' , ... SIPREFIXES , 'P' , 'E' , 'Z' , 'Y' , 'R' , 'Q' ] ;
20702070
2071- function isSIFormat ( exponentFormat ) {
2072- return exponentFormat === 'SI' || exponentFormat === 'SI extended' || exponentFormat === 'B' ;
2073- }
2071+ const isSIFormat = ( exponentFormat ) => [ 'SI' , 'SI extended' , 'B' ] . includes ( exponentFormat ) ;
20742072
20752073// are we beyond the range of common SI prefixes?
20762074// 10^-16 -> 1x10^-16
@@ -2083,10 +2081,26 @@ function beyondSI(exponent) {
20832081 return exponent > 14 || exponent < - 15 ;
20842082}
20852083
2084+
2085+ // are we beyond the range of all SI prefixes?
2086+ // 10^-31 -> 1x10^-31
2087+ // 10^-30 -> 1q
2088+ // 10^-29 -> 10q
2089+ // ...
2090+ // 10^31 -> 10Q
2091+ // 10^32 -> 100Q
2092+ // 10^33 -> 1x10^33
20862093function beyondSIExtended ( exponent ) {
20872094 return exponent > 32 || exponent < - 30 ;
20882095}
20892096
2097+ function shouldSwitchSIToPowerFormat ( exponent , exponentFormat ) {
2098+ if ( ! isSIFormat ( exponentFormat ) ) return false ;
2099+ if ( exponentFormat === 'SI extended' && beyondSIExtended ( exponent ) ) return true ;
2100+ if ( exponentFormat !== 'SI extended' && beyondSI ( exponent ) ) return true ;
2101+ return false ;
2102+ }
2103+
20902104function numFormat ( v , ax , fmtoverride , hover ) {
20912105 var isNeg = v < 0 ;
20922106 // max number of digits past decimal point to show
@@ -2162,8 +2176,7 @@ function numFormat(v, ax, fmtoverride, hover) {
21622176
21632177 // add exponent
21642178 if ( exponent && exponentFormat !== 'hide' ) {
2165- if ( ( isSIFormat ( exponentFormat ) && exponentFormat !== 'SI extended' && beyondSI ( exponent ) ) ||
2166- ( isSIFormat ( exponentFormat ) && exponentFormat === 'SI extended' && beyondSIExtended ( exponent ) ) ) exponentFormat = 'power' ;
2179+ if ( shouldSwitchSIToPowerFormat ( exponent , exponentFormat ) ) exponentFormat = 'power' ;
21672180
21682181 var signedExponent ;
21692182 if ( exponent < 0 ) signedExponent = MINUS_SIGN + - exponent ;
@@ -2177,11 +2190,9 @@ function numFormat(v, ax, fmtoverride, hover) {
21772190 } else if ( exponentFormat === 'B' && exponent === 9 ) {
21782191 v += 'B' ;
21792192 } else if ( isSIFormat ( exponentFormat ) ) {
2180- if ( exponentFormat !== 'SI extended' ) {
2181- v += SIPREFIXES [ exponent / 3 + 5 ] ;
2182- } else if ( exponentFormat === 'SI extended' ) {
2183- v += SIPREFIXES_EXTENDED [ exponent / 3 + 10 ] ;
2184- }
2193+ v += exponentFormat === 'SI extended'
2194+ ? SIPREFIXES_EXTENDED [ exponent / 3 + 10 ]
2195+ : SIPREFIXES [ exponent / 3 + 5 ] ;
21852196 }
21862197 }
21872198
0 commit comments