npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zh-to-number

v0.0.1

Published

中文数字转阿拉伯数字

Downloads

29

Readme

Zh-to-number

NPM version Downloads License GitHub stars

中文数字转阿拉伯数字

Highlights

  • 📦 零依赖:无需依赖其他库或框架。
  • ⚙️ 支持 ESM、CJS 和 IIFE 引入:可根据项目需求选择合适的引入方式。
  • 📝 支持简体中文和繁体中文的大小写数字:满足不同语境下的需求。
  • 🔢 大数支持:显示大数无精度问题,保持精确性。
  • 🔧 可自定义配置:根据个人需求调整库的行为和设置。

Install

npm install zh-to-number

Usage

为了避免大数精度丢失问题,阿拉伯数字作为字符串返回

import { zhToNumber } from "zh-to-number";

zhToNumber("你好");                                         // NaN
zhToNumber("零");                                           // "0"
zhToNumber("零点一");                                       // "0.1"
zhToNumber("一千万零一");                                   // "10000001"
zhToNumber("一千二百三十四万五千六百七十八");                   // "12345678"
zhToNumber("一十");                                        // "10"
zhToNumber("十");                                         // "10"
zhToNumber("壹仟万零壹", { language: "zh-CN-uppercase" }); // "10000001"

CDN

你可以借助 script 标签直接通过 CDN 来使用:

<script src="https://unpkg.com/zh-to-number/dist/zh-to-number.global.js"></script>
<script>console.log(__ZH_TO_NUMBER__.zhToNumber("一十"));</script>

这里我们使用了 unpkg,但你也可以使用任何提供 npm 包服务的 CDN,例如 jsdelivrcdnjs。当然,你也可以下载此文件并自行提供服务。

自定义数级

默认最大支持到 10^16,即千万亿,最小支持到 10^-16,如果使用万万和亿亿则没有限制。如果超出数级则会返回 NaN,不过可以自定义数级。

import { zhToNumber, RESOURCES } from "zh-to-number";

const options = {
	language: "zh-CN-lowercase" as const,
	magnitudeList: [...RESOURCES["zh-CN-lowercase"].magnitudeList, "京"],
};
zhToNumber("一京", options) // "10000000000000000"

API

zhToNumber(inputNumberString, options?)

inputNumberString

Type: string

需要转换的中文数字,即使是超大数字也无精度丢失问题

options

language

Type: "zh-CN-lowercase" | "zh-CN-uppercase" | "zh-TW-lowercase" | "zh-TW-uppercase" | "zh-HK-lowercase" | "zh-HK-uppercase"
Default: "zh-CN-lowercase"

不同的中文数字转为阿拉伯数字,HK 和 TW 没有区别都表示繁体中文。

thousandsSeparator

Type: boolean
Default: false

添加千分分隔符。

digitsList

Type: string[]
Default: ["", "十", "百", "千"]

对应语言的数位列表。

magnitudeList

Type: string[]
Default: ["", "万", "亿", "兆"]

对应语言的数级列表。

baseNumerals

Type: string[]
Default: ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]

对应语言从零到九的中文数字列表。

minusSign

Type: string
Default:

对应语言「负」的写法。

positive

Type: string
Default:

对应语言「正」的写法。

decimalPoint

Type: string
Default:

对应语言「点」的写法。

Related

License

MIT License © 2023-Present Condor Hero