# @codecompose/words-to-numbers 中文文档

规范地址：https://npmcn.com/package/%40codecompose/words-to-numbers/

版本：2\.0\.1
npm 元数据更新时间：2024-03-08T08:12:31.141Z
中文译文更新时间：2026-11-10T06:27:31.613Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40codecompose/words-to-numbers
原文对照：https://npmcn.com/package/%40codecompose/words-to-numbers/?lang=original
项目主页：https://github.com/0x80/words-to-numbers#readme
源码仓库：https://github.com/0x80/words-to-numbers

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：28.8 kB，11 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2024-02-15T17:51:12.167Z
- 最新版本发布：2024-03-08T08:12:30.706Z
- 近 30 天下载量：8067（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

将文本单词转换为数字, 并带有可选模糊文本匹配

## 安装

```sh
npm install @codecompose/words-to-numbers
```

也可以使用：`pnpm add @codecompose/words-to-numbers`、`yarn add @codecompose/words-to-numbers`、`bun add @codecompose/words-to-numbers`

## README

# 单词到数字

这个寄存器来自 [字从数](https://github.com/finnfiddle/words-to-numbers) 因为 项目似乎被放弃了.

代码库经过了现代化,在 2. 0 下发布,并进行了以下修改: 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码库 代码 代码库 代码库 代码库

-   修正无效的软件包列表
-   删除转写并完成 [纯无害环境管理](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)
-   添加打字符 [类型推论](https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html)
-   删除陈旧和陈旧的依赖关系
-   使用 Vitest 替换旧的测试工具
-   将 Clj- fuzy 升级到最新版本
-   添加更美的格式
-   使用 PNPM 作为软件包管理器

我以后可能会把代码转换成打字机 但代码质量很差 我不确定这是否值得 欢迎公关.

我还用PNPM取代了国家预防机制,这是不必要的,但仅仅是针对个人的 优先选择.

## 使用量

```
npm install @codecompose/words-to-numbers
```

如果整个字符串是一个数字,它就会返回一个 `Number` 类型 否则它会带着所有数字实例返回原始字符串 替换.

TODO: 为解析混合数字和单词添加功能. 公关欢迎.

## 基本实例

```javascript
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("one hundred"); //100
wordsToNumbers("one hundred and five"); //105
wordsToNumbers("one hundred and twenty five"); //125
wordsToNumbers("four thousand and thirty"); //4030
wordsToNumbers("six million five thousand and two"); //6005002
wordsToNumbers("a thousand one hundred and eleven"); //1111
wordsToNumbers("twenty thousand five hundred and sixty nine"); //20569
wordsToNumbers("five quintillion"); //5000000000000000000
wordsToNumbers("one-hundred"); //100
wordsToNumbers("one-hundred and five"); //105
wordsToNumbers("one-hundred and twenty-five"); //125
wordsToNumbers("four-thousand and thirty"); //4030
wordsToNumbers("six-million five-thousand and two"); //6005002
wordsToNumbers("a thousand, one-hundred and eleven"); //1111
wordsToNumbers("twenty-thousand, five-hundred and sixty-nine"); //20569
```

## 字符串中的多个数字

返回所有已替换实例的字符串 .

```javascript
wordsToNumbers('there were twenty-thousand, five-hundred and sixty-nine X in the five quintillion Y')) // 'there were 20569 X in the 5000000000000000000 Y'
```

## 与模糊匹配

用途 [雅罗距离](http://yomguithereal.github.io/clj-fuzzy/javascript.html#jaro) 以找到数字词的最佳匹配。 不要完全依赖这个 准确...

```javascript
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("won huntred", { fuzzy: true }); //100
wordsToNumbers("too thousant and fiev", { fuzzy: true }); //2005
wordsToNumbers("tree millyon sefen hunderd and twinty sex", { fuzzy: true }); //3000726
```

## 小数点

```javascript
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("ten point five"); //10.5
wordsToNumbers("three point one four one five nine two six"); //3.1415926
```

## 数字

```javascript
import wordsToNumbers from "words-to-numbers";
wordsToNumbers("first"); //1
wordsToNumbers("second"); //2
wordsToNumbers("third"); //3
wordsToNumbers("fourteenth"); //14
wordsToNumbers("twenty fifth"); //25
wordsToNumbers("thirty fourth"); //34
wordsToNumbers("forty seventh"); //47
wordsToNumbers("fifty third"); //53
wordsToNumbers("sixtieth"); //60
wordsToNumbers("seventy second"); //72
wordsToNumbers("eighty ninth"); //89
wordsToNumbers("ninety sixth"); //96
wordsToNumbers("one hundred and eighth"); //108
wordsToNumbers("one hundred and tenth"); //110
wordsToNumbers("one hundred and ninety ninth"); //199
```

## 隐蔽的上百人

```javascript
wordsToNumbers("nineteen eighty four", { impliedHundreds: true }); //1984
wordsToNumbers("one thirty", { impliedHundreds: true }); //130
wordsToNumbers("six sixty two", { impliedHundreds: true }); //662
wordsToNumbers("ten twelve", { impliedHundreds: true }); //1012
wordsToNumbers("nineteen ten", { impliedHundreds: true }); //1910
wordsToNumbers("twenty ten", { impliedHundreds: true }); //2010
wordsToNumbers("twenty seventeen", { impliedHundreds: true }); //2017
wordsToNumbers("twenty twenty", { impliedHundreds: true }); //2020
wordsToNumbers("twenty twenty one", { impliedHundreds: true }); //2021
wordsToNumbers("fifty sixty three", { impliedHundreds: true }); //5063
wordsToNumbers("fifty sixty", { impliedHundreds: true }); //5060
wordsToNumbers("fifty sixty three thousand", { impliedHundreds: true }); //5063000
wordsToNumbers("one hundred thousand", { impliedHundreds: true }); //100000
```

## 常见问题

### @codecompose/words-to-numbers 是什么？

将文本单词转换为数字, 并带有可选模糊文本匹配

### 如何安装 @codecompose/words-to-numbers？

在项目目录运行 npm install @codecompose/words-to-numbers，或使用 pnpm add / yarn add / bun add。

### @codecompose/words-to-numbers 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @codecompose/words-to-numbers 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @codecompose/words-to-numbers 可以商用吗？

@codecompose/words-to-numbers 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @codecompose/words-to-numbers 的下载量有多少？

根据 npm 官方下载统计，@codecompose/words-to-numbers 在 2026/10/10 – 2026/11/08 这 30 天内被下载 8,067 次，日均约 269 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @codecompose/words-to-numbers 还在维护吗？

最新版本 2\.0\.1 发布于 2024/03/08（2 年前）；至今共发布 4 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/02/16。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

