
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
一个高性能的中文数字与阿拉伯数字互转库,支持 TypeScript
npm install cn-nm
yarn add cn-nm
pnpm add cn-nm
import { toCn, toNm } from 'cn-nm';
// 数字转中文
console.log(toCn(123)); // 壹佰贰拾叁
console.log(toCn(1024)); // 壹仟零贰拾肆
// 中文转数字
console.log(toNm('壹佰贰拾叁')); // 123
console.log(toNm('壹仟零贰拾肆')); // 1024
const { toCn, toNm } = require('cn-nm');
toCn(123); // 壹佰贰拾叁
toNm('壹佰贰拾叁'); // 123
import { toCn, toNm } from 'cn-nm';
// 完整的 TypeScript 类型支持
const chineseNumber: string = toCn(123);
const arabicNumber: number = toNm('壹佰贰拾叁');
toCn(num: number | string): string将阿拉伯数字转换为中文数字
参数:
num - 要转换的数字(数字类型或字符串类型)返回值:
string - 转换后的中文数字字符串示例:
toCn(123) // "壹佰贰拾叁"
toCn(1000) // "壹仟"
toCn(10001) // "壹万零壹"
toCn(123.45) // "壹佰贰拾叁点肆伍"
toCn("999999999") // "玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖"
toNm(chineseStr: string): number将中文数字转换为阿拉伯数字
参数:
chineseStr - 中文数字字符串返回值:
number - 转换后的阿拉伯数字示例:
toNm('壹佰贰拾叁') // 123
toNm('壹仟') // 1000
toNm('壹万零壹') // 10001
toNm('壹佰贰拾叁点肆伍') // 123.45
toNm('拾') // 10
toNm('佰') // 100
const amount = 1234.56;
const chineseAmount = toCn(amount);
console.log(`人民币 ${chineseAmount} 元整`);
// 人民币 壹仟贰佰叁拾肆点伍陆 元整
// 对于超大数字,建议使用字符串形式传入
const largeNumber = "123456789012345";
const chineseLarge = toCn(largeNumber);
console.log(chineseLarge);
// 壹佰贰拾叁万肆仟伍佰陆拾柒亿捌仟玖佰零壹万贰仟叁佰肆拾伍
function validateChineseNumber(input) {
const number = toNm(input);
// 有效的中文数字会返回对应数值,无效输入返回0
// 注意:"零"是有效输入,会返回0,需要特殊判断
return number > 0 || input === '零';
}
console.log(validateChineseNumber('壹佰')); // true
console.log(validateChineseNumber('零')); // true
console.log(validateChineseNumber('无效输入')); // false
console.log(validateChineseNumber('壹壹')); // false (连续重复字符)
数字范围:
toCn 支持最大约 999999999999 (万亿级别)输入验证:
toNm 严格验证中文数字格式,符合生活实际逻辑小数支持:
单位层级:
git clone https://github.com/2ue/cn-nm.git
cd cn-nm
npm install
# 运行测试
npm test
# 代码质量检查
npm run lint
# 类型检查
npm run type-check
# 格式化代码
npm run format
# 构建项目
npm run build
# 监听模式开发
npm run dev
npm run test:coverage
欢迎提交 Issue 和 Pull Request!
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Copyright (c) 2024-present, 2ue
⭐ 如果这个项目对你有帮助,请给个 star!
FAQs
A tool is that can inner-transformation Number and Chinese
We found that cn-nm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.