chinese-numbering
v1.0.0
Published
format numbers as chinese words
Downloads
525
Readme
chinese-numbering
format numbers as chinese words and ordinals
Install
npm install chinese-numbering
yarn add chinese-numbering
Usage
import
Node.js
const cn = require("chinese-numbering"); console.log(cn.numberToChinese(11));
Browser
with iife / umd bundle:
<script src="https://unpkg.com/[email protected]/bundle/iife.js"></script> <script> const cn = window.ChineseNumbering; alert("ChineseNumbering is formatting 11 to " + cn.numberToChinese(11)); </script>
with es module bundle:
<script type="module"> import * as cn from "https://unpkg.com/[email protected]/bundle/es.js"; alert("ChineseNumbering is formatting 11 to " + cn.numberToChinese(11)); </script>
ES Modules
import * as cn from "chinese-numbering"; console.log(cn.numberToChinese(11));
number to Simplified / Traditional Chinese
// Simplified Chinese 简体中文
// 一万零一点六
cn.numberToChinese(10001.6);
cn.numberToChinese(10001.6, "s");
cn.numberToChinese(10001.6, "simplified");
cn.numberToChinese(10001.6, { chineseType: "simplified" });
// Tradition Chinese 繁體中文
// 一萬零一點六
cn.numberToChinese(10001.6, "t");
cn.numberToChinese(10001.6, "traditional");
cn.numberToChinese(10001.6, { chineseType: "traditional" });
// negative numbers 负数
cn.numberToChinese(-1200); // 负一千二百
cn.numberToChinese(-0.5, "t"); // 負零點五
Keep the first One
// not keep the first `One`
cn.numberToChinese(12); // 十二
// Keep the first `One`
cn.numberToChinese(12, { keepOne: true }); // 一十二
Ordinal
cn.numberToChineseOrdinal(12); // 第十二
cn.numberToChineseOrdinal(12, { keepOne: true }); // 第一十二
cn.numberToChineseOrdinal(10000, { chineseType: "t" }); // 第一萬
// with Arabic numerals
cn.numberToChineseOrdinalWithArabic(12); // 第12
License
MIT © Equal Ma
The algorithm to format number as Chinese words is inspired by digi3studio/number-to-chinese-words. Great Thanks to digi3studio.