@@ -3,6 +3,7 @@ export { JpNumeralUnit, NumeralObj, Numerals, Sign }
33
44const EPSILON = 1e-10
55const log10 = ( v : number ) => Math . log ( v ) / Math . log ( 10 )
6+ // for IE
67
78export class Numeral {
89 raw : number
@@ -67,24 +68,26 @@ export const numerals = (n: number, base: JpNumeralUnit = JpNumeralUnit.零): Nu
6768 const unitLen = Object . keys ( JpNumeralUnit ) . length / 2 // maximum unit
6869 const numberLen = Math . ceil ( log10 ( abs ) / 4 + EPSILON )
6970 const len = Math . min ( unitLen , numberLen )
70- const numerals = new Array ( len )
71+ const nums = new Array ( len )
7172 . fill ( NaN )
7273 . map ( ( _ , i ) => ( i === 0 ? new NumeralZero ( i , abs ) : new Numeral ( i , abs ) ) )
7374 . reverse ( )
7475
7576 return {
76- toNumerals : ( ) => numerals ,
77- toTuples : ( ) => numerals . map ( numeral => numeral . toTuple ( ) ) ,
78- toNumeralObjs : ( ) => numerals . map ( numeral => numeral . toNumeralObj ( ) ) ,
79- toString : ( ) => numerals . reduce ( ( s , numeral ) => `${ s } ${ numeral } ` , '' ) ,
77+ toNumerals : ( ) => nums ,
78+ toTuples : ( ) => nums . map ( numeral => numeral . toTuple ( ) ) ,
79+ toNumeralObjs : ( ) => nums . map ( numeral => numeral . toNumeralObj ( ) ) ,
80+ toString : ( ) => nums . reduce ( ( s , numeral ) => `${ s } ${ numeral } ` , '' ) ,
8081 toAbsNumber : ( ) => abs ,
8182
8283 sign : ( ) => sign ,
83- toSignedNumerals : ( ) => [ sign , numerals ] ,
84- toSignedTuples : ( ) => [ sign , numerals . map ( numeral => numeral . toTuple ( ) ) ] ,
85- toSignedNumeralObjs : ( ) => [ sign , numerals . map ( numeral => numeral . toNumeralObj ( ) ) ] ,
86- toSignedString : ( ) => numerals . reduce ( ( s , numeral ) => `${ s } ${ numeral } ` , sign === - 1 ? '-' : '' ) ,
87- toNumber : ( ) => sign * abs
84+ toSignedNumerals : ( ) => [ sign , nums ] ,
85+ toSignedTuples : ( ) => [ sign , nums . map ( numeral => numeral . toTuple ( ) ) ] ,
86+ toSignedNumeralObjs : ( ) => [ sign , nums . map ( numeral => numeral . toNumeralObj ( ) ) ] ,
87+ toSignedString : ( ) => nums . reduce ( ( s , numeral ) => `${ s } ${ numeral } ` , sign === - 1 ? '-' : '' ) ,
88+ toNumber : ( ) => sign * abs ,
89+
90+ round : ( base : JpNumeralUnit ) => numerals ( sign * Math . round ( abs / Math . pow ( 10 , base * 4 ) ) , base )
8891 }
8992}
9093
0 commit comments