import Decimal from "break_infinity.js" ; import { Notation } from "../notation" ; const ChineseNotPrefixes = [ "" , "万" , "亿" , "兆" , "京" , "垓" , "秭" , "穰" , "沟" , "涧" , "正" , "载" , "极" ] ; const ChineseNotDigits = [ "〇" , "一" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九" ] ; const ChineseNotPlaces = [ "" , "十" , "百" , "千" ] ; export class ChineseNotation extends Notation { public get name ( ) : string { return "Chinese" ; } public get infinite ( ) : string { return "無窮" ; } public formatUnder1000 ( value ) { return this . formatUnder10000 ( value ) ; } public formatDecimal ( value ) { return this . formatChinese ( value ) ; } private formatChinese ( value ) { if ( value . exponent < 4 ) { return this . formatUnder10000 ( value . toNumber ( ) ) ; } if ( value . exponent < 52 ) { return this . formatAbove10000 ( value ) ; } const replacement = Math . floor ( value . exponent / 48 ) ; if ( replacement < 6 ) { return this . formatAbove1e48 ( value . div ( Decimal . pow ( 1e48 , replacement ) ) ) + ChineseNotPrefixes [ 12 ] . repeat ( replacement ) ; } return `${ this . formatAbove1e48 ( value . div ( Decimal . pow ( 1e48 , replacement ) ) ) }(${ this . formatChinese ( new Decimal ( replacement ) ) })${ ChineseNotPrefixes [ 12 ] }` ; } private formatAbove1e48 ( value ) { const exp = Math . floor ( value . exponent / 4 ) * 4 ; const man = value . mantissa * 10 ** ( value . exponent - exp ) ; return this . formatUnder10000WithPlaces ( man ) + ChineseNotPrefixes [ exp / 4 ] ; } private formatAbove10000 ( value ) { const exp = Math . floor ( value . exponent / 4 ) * 4 ; const man = Math . floor ( value . mantissa * 10 ** ( value . exponent - exp ) ) ; const manb = Math . floor ( value . mantissa * 10 ** ( value . exponent - exp + 4 ) ) % 10000 ; return this . formatUnder10000 ( man ) + ChineseNotPrefixes [ exp / 4 ] + ( manb > 0 ? this . formatUnder10000 ( manb ) + ChineseNotPrefixes [ exp / 4 - 1 ] : "" ) ; } private formatUnder10000WithPlaces ( value ) { return `${ this . formatUnder1000 ( value ) }點${ [ 1 , 2 , 3 ] . map ( ( x ) => ChineseNotDigits [ Math . floor ( value * 10 ** x ) % 10 ] ) . join ( "" ) }` ; } private formatUnder10000 ( value ) { return [ 3 , 2 , 1 , 0 ] . map ( ( x ) : string => { const digit = Math . floor ( value / 10 ** x ) % 10 ; if ( digit === 0 ) { return "" ; } if ( digit === 1 && x === 1 ) { return ChineseNotPlaces [ x ] ; } return ChineseNotDigits [ digit ] + ChineseNotPlaces [ x ] ; } ) . join ( "" ) || ChineseNotDigits [ 0 ] ; } }