lib-arabic-chinese
A library for converting from Arabic numbers to Chinese numbers. For example, 8,520,342,747,330,570
to 捌仟伍佰貳拾兆參仟肆佰貳拾柒億肆仟柒佰參拾參萬零伍佰柒拾
.
Features
- Theoretically support up to
10^44
. (For now, testings only cover from10^0
to10^16
). - Support
TypeScript@^3
. - Support
Node.js
andbrowser
. - Support uppercase(ex.
壹貳參
) or lowercase(ex.一二三
). - Support customized prefix/suffix.
- Support customized output style of signed symbols.
- Support
Nubmer
andBigInt
. - Support
Traditional
andSimplified
Chinese.
Installation
# install by yarn yarn add lib-arabic-chinese # or install by npm npm install lib-arabic-chinese
Usage
Simple and easy to use:
;; // simple usageconst test1 = ;assert; // auto drop commasconst test2 = ;assert;
Full options for customizing:
// lower case or upper case, default: upperconst test3 = ;assert; // suffix and prefix, default: empty stringconst test4 = ;assert; const test5 = ;assert; // show signed or not, default: hide plus signed, show minus signedconst test6 = ;assert; const test7 = ;assert; // flexible prefix position with signed, default: prefix will be placed after signedconst test8 = ;assert; // Able to customize the output of signed symbols, default: 正 for plus, 負 for minusconst test9 = ;assert; // Support Simplified/Traditional Chinese, default: `zh-tw`const test10 = ;assert;
Important
: BigInt and Number
The conveter could be passed Number
or BigInt
now. But it only calls Number.prototype.toString
and BigInt.prototype.toString
methods to make a String
. Then it might be occurred some unexpected behaviours while it calls if the number is overflowed.
For example:
> '9007199254740991'> '590295810358705700000'> '1.1805916207174113e+21'> '1180591620717411303424'
Node.js
will convert Number(Math.pow(2,70))
into 1.1805916207174113e+21
. It is unable to convert to Chinese number format. So, the converter now will throw an error. But if it is BigInt
, it will be safely to convert to a clearly number. However, it also has to be mentioned the overflow issue with BigInt
.
Options
The default configuration options would be:
caseType: 'upper' // `upper` | `lower` lang: 'zh-tw' // 'zh-tw' | 'zh-cn' showPlusSigned: false showMinusSigned: true signedOutput: prefix: '' suffix: '' prefixPosition: 'after-signed' // `after-signed` | `before-signed`
Todo & Roadmap
- Support Simple Chinese. (ex.
億/亿
) - Support Positive numbers (integer).
- Support Negative numbers (integer).
- Support Floating numbers. (ex.
3.14
to三點一四
) - Support Fraction numbers. (ex.
4/5
to五分之四
) - Support Mixing numbers. (ex.
4,500萬
to四千五百萬
) - Support Metric unit rule.