
开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载Humanizer 的NumberToWordsExtension是一个面向所有 .NET 应用的静态扩展类负责把整数转换为本地化的序数词ordinal、基数词cardinal与元组名tuple例如1 - one、1 - first、1 - single。本指南以 version-3.0.10 的 API 文档 为主体结合当前仓库源码系统讲解该类全部 12 个公开重载的签名、参数语义、多语言西语、俄语、希伯来语、巴西葡语等行为差异与底层实现机制帮助你准确选型并写出可直接运行的本地化代码。类概览一个入口、三种转换能力NumberToWordsExtension是public static class位于命名空间Humanizer继承关系为System.Object - NumberToWordsExtension本身不可实例化全部通过扩展方法语法调用。其完整实现位于 src/Humanizer/NumberToWordsExtension.cs。从源码结构看它对外暴露三大类转换能力能力典型调用输出示例en-US基数词3501.ToWords()three thousand five hundred and one序数词1.ToOrdinalWords()first元组名1.ToTuple()single所有方法都遵循同一个设计原则文化感知culture-aware。文档中反复强调每个重载都接受CultureInfo? culture null当传入null时使用当前线程的 UI 文化current threads UI culture。输出不仅包含 locale 特有的量级词如印度英语的 lakh、crore还区分英语系内部差异en、en-GB、en-IN。核心机制Configurator 与转换器注册表所有扩展方法最终都收敛到同一个调用点——Configurator.GetNumberToWordsConverter(culture)它返回一个INumberToWordsConverter实例再委托其Convert(...)、ConvertToOrdinal(...)、ConvertToTuple(...)方法完成实际转换。见 src/Humanizer/Configuration/Configurator.csinternal static INumberToWordsConverter GetNumberToWordsConverter(CultureInfo? culture) NumberToWordsConverters.ResolveForCulture(culture);其中NumberToWordsConverters是 NumberToWordsConverterRegistry继承LocaliserRegistryINumberToWordsConverter它的默认后备fallback是NumberToWordsProfileCatalog.Resolve(en, CultureInfo.InvariantCulture)即当某种文化没有注册专属转换器时自动回退到英语en实现。这一点可以推断出即使你传入一个未覆盖的冷门 culture也不会抛异常而是得到英语结果。INumberToWordsConverter接口src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs定义了完整的转换契约Convert(long number)基数词使用 locale 默认语法性别Convert(long number, bool addAnd)可选地插入 locale 专属连词Convert(long number, GrammaticalGender gender, bool addAnd true)按指定语法性别Convert(long number, WordForm wordForm, GrammaticalGender gender, bool addAnd true)性别 词形ConvertToOrdinal(int number)/ConvertToOrdinal(int number, WordForm wordForm)/ConvertToOrdinal(int number, GrammaticalGender gender, WordForm wordForm)序数词ConvertToTuple(int number)元组名仓库 src/Humanizer/Localisation/NumberToWords 下提供了 40 个具体转换器实现如GenderedNumberToWordsConverter、EastSlavicNumberToWordsConverter、IndianGroupingNumberToWordsConverter等它们按 locale 组合出不同的数词策略这正是同一个 API、不同语言输出不同结果的底层来源。ToWords 系列基数词的六个重载ToWords把数字转换为基数词是全类中重载最丰富的方法覆盖int与long两种输入类型。原文档给出的核心签名与行为如下。默认形式无参 culturepublic static string ToWords(this int number, CultureInfo? culture null); public static string ToWords(this long number, CultureInfo? culture null, bool addAnd true);文档示例3501.ToWords()→three thousand five hundred and one。注意long版本带有默认值addAnd true即默认在最后一个数段前插入连词and而int版本通过内部((long)number).ToWords(culture)转发见 源码因此同样默认带and。若想得到美式支票风格的three thousand five hundred one使用3501.ToWords(false)即下面的 addAnd 重载。显式控制连词addAndpublic static string ToWords(this int number, bool addAnd, CultureInfo? culture null);文档示例3501.ToWords(false)→three thousand five hundred one。addAnd的语义是是否在最后一个数字前加入 and这个连词会随 locale 变化英语用and法语用et等具体由 locale 的转换器决定。词形变化WordFormpublic static string ToWords(this int number, WordForm wordForm, CultureInfo? culture null); public static string ToWords(this long number, WordForm wordForm, CultureInfo? culture null, bool addAnd false);文档以西班牙语为例——西语中以 1 结尾的数字会随其在句中的位置改变形态21.ToWords(WordForm.Normal) - veintiuno // as in Mi número favorito es el veintiuno. 21.ToWords(WordForm.Abbreviation)- veintiún // as in En total, conté veintiún cochesWordForm是 src/Humanizer/WordForm.cs 中定义的三值枚举Normal单词的常规书写形态默认Abbreviation单词的缩短形态如西语 tercero → tercerEifelerEifeler 规则形态基于卢森堡语 Eifeler 拼写规则见 src/Humanizer/Localisation/EifelerRule.cs值得注意long版本的ToWords(WordForm, ...)默认addAnd false与无参版本默认值不同使用时应留意。语法性别GrammaticalGenderpublic static string ToWords(this int number, GrammaticalGender gender, CultureInfo? culture null); public static string ToWords(this long number, GrammaticalGender gender, CultureInfo? culture null);文档给出俄语与希伯来语的示例// Russian 1.ToWords(GrammaticalGender.Masculine) - один 1.ToWords(GrammaticalGender.Feminine) - одна // Hebrew 1.ToWords(GrammaticalGender.Masculine) - אחד 1.ToWords(GrammaticalGender.Feminine) - אחתGrammaticalGender枚举定义于 src/Humanizer/GrammaticalGender.cs取值Masculine、Feminine、Neuter。需要强调该参数只在支持性别区分的 locale 生效俄语、希伯来语、西语、巴西葡语等英语等无性别语言会忽略它。词形 性别组合完整形态public static string ToWords(this int number, WordForm wordForm, GrammaticalGender gender, CultureInfo? culture null); public static string ToWords(this long number, WordForm wordForm, GrammaticalGender gender, CultureInfo? culture null);文档示例西班牙语词形与性别同时生效21.ToWords(WordForm.Normal, GrammaticalGender.Masculine) - veintiuno // as in Mi número favorito es el veintiuno. 21.ToWords(WordForm.Abbreviation, GrammaticalGender.Masculine)- veintiún // as in En total, conté veintiún coches 21.ToWords(WordForm.Normal, GrammaticalGender.Feminine) - veintiuna // as in veintiuna personas从 源码 可见该组合最终调用Configurator.GetNumberToWordsConverter(culture).Convert(number, wordForm, gender)即addAnd采用接口默认值true。long 专属重载的注意点public static string ToWords(this long number, WordForm wordForm, CultureInfo? culture null, bool addAnd false); public static string ToWords(this long number, CultureInfo? culture null, bool addAnd true);这两个long重载把addAnd作为具名可选参数放在 culture 之后允许你同时控制词形与连词。这正是int版本ToWords(bool addAnd, WordForm wordForm, CultureInfo?)源码中转发到((long)number).ToWords(wordForm, culture, addAnd)的实现基础。建议调用时使用具名参数如.ToWords(wordForm: WordForm.Normal, addAnd: true)以避免参数顺序歧义。ToOrdinalWords 系列序数词的四个重载ToOrdinalWords把数字转换为序数词同样全部支持 culture 参数culture null时使用当前线程 UI 文化。最简形式public static string ToOrdinalWords(this int number, CultureInfo? culture null);文档示例1.ToOrdinalWords()→first。仓库测试 tests/Humanizer.Tests/NumberToWordsTests.cs 提供了 en-US 下从0 - zeroth到1000000 - millionth的完整映射包括21 - twenty-first、112 - hundred and twelfth、10121 - ten thousand one hundred and twenty-first等复合形式。按词形WordFormpublic static string ToOrdinalWords(this int number, WordForm wordForm, CultureInfo? culture null);文档示例西班牙语1.ToOrdinalWords(WordForm.Normal) - primero // As in He llegado el primero. 3.ToOrdinalWords(WordForm.Abbreviation) - tercer // As in Vivo en el tercer piso按语法性别GrammaticalGenderpublic static string ToOrdinalWords(this int number, GrammaticalGender gender, CultureInfo? culture null);文档以巴西葡萄牙语为例1.ToOrdinalWords(GrammaticalGender.Masculine) - primeiro 1.ToOrdinalWords(GrammaticalGender.Feminine) - primeira性别 词形组合public static string ToOrdinalWords(this int number, GrammaticalGender gender, WordForm wordForm, CultureInfo? culture null);文档示例西班牙语注意缩写形态在阴性时不变3.ToOrdinalWords(GrammaticalGender.Masculine, WordForm.Normal) - tercero 3.ToOrdinalWords(GrammaticalGender.Masculine, WordForm.Abbreviation) - tercer 3.ToOrdinalWords(GrammaticalGender.Feminine, WordForm.Normal) - tercera 3.ToOrdinalWords(GrammaticalGender.Feminine, WordForm.Abbreviation) - tercera特殊的 first 本地化捷径源码 src/Humanizer/NumberToWordsExtension.cs#L126-L149 中有一个值得注意的细节当number 1且显式传入了 culture 时ToOrdinalWords会先尝试TryGetNativeFirstOrdinalWord直接返回以下语言的第一母语形式而不再走通用转换器丹麦语daførste菲律宾语filuna印尼语id、马来语mspertama斯洛伐克语sk按性别区分prvý阳/prvá阴/prvé中祖鲁语zuokokuqala其余数字仍委托ConvertToOrdinal(...)。这解释了为什么部分语言的 1 序数词与其它数字走的是两条代码路径。ToTuple数字到元组名的转换public static string ToTuple(this int number, CultureInfo? culture null);文档示例1.ToTuple()→single。该方法把数字转换为英语中用于描述几元组的拉丁词根名称底层调用Configurator.GetNumberToWordsConverter(culture).ConvertToTuple(number)源码。仓库测试 NumberToWordsTests.cs#L107-L122 给出了完整映射0.ToTuple() - 0-tuple // 数字回退形式 1.ToTuple() - single 2.ToTuple() - double 3.ToTuple() - triple 4.ToTuple() - quadruple 5.ToTuple() - quintuple 6.ToTuple() - sextuple 7.ToTuple() - septuple 8.ToTuple() - octuple 9.ToTuple() - nonuple 10.ToTuple() - decuple 100.ToTuple() - centuple 1000.ToTuple() - milluple当数字没有对应的拉丁词根名如 0、11 等时接口契约规定返回数字回退形式如0-tuple这正是ConvertToTuple在 INumberToWordsConverter 中声明当 locale 定义了元组名则返回名称否则返回数字回退的原因。实战组合如何按需选型综合以上重载可以按需求维度建立选型决策需求推荐调用展示数字的英文读法含 and3501.ToWords()美式风格、不含 and3501.ToWords(false)西语/俄语等需要性别一致.ToWords(GrammaticalGender.Feminine)或.ToWords(WordForm.Abbreviation, GrammaticalGender.Masculine)西语veintiún coches这类缩写.ToWords(WordForm.Abbreviation)排名、楼层等序数表达.ToOrdinalWords()、.ToOrdinalWords(WordForm.Abbreviation)需要本地化序数.ToOrdinalWords(new CultureInfo(es))描述几元组数据结构.ToTuple()多语言场景示例结合测试数据 NumberToWordsTests.cs#L124-L13011.ToWords(new CultureInfo(en-US)) // eleven 22.ToWords(new CultureInfo(ar)) // اثنان و عشرون 40.ToWords(new CultureInfo(ru)) // сорок 1021.ToWords(new CultureInfo(hr)) // tisuću dvadeset jedan 11.ToWords(new CultureInfo(ta)) // பதினொன்று 1021.ToOrdinalWords(new CultureInfo(en-US)) // thousand and twenty-first 21.ToOrdinalWords(new CultureInfo(ar)) // الحادي و العشرون 1112.ToOrdinalWords(new CultureInfo(ru)) // одна тысяча сто двенадцатый测试验证与边界行为仓库对NumberToWordsExtension的行为做了系统化测试主要位于 tests/Humanizer.Tests/NumberToWordsTests.cs测试类标注[UseCulture(en-US)]确保测试环境文化一致。可关注几个边界结论WordForm 对部分语言被忽略ToOrdinalWords_WordFormIsIgnored与ToOrdinalWords_WordFormIsIgnoredWithSpecificCulture测试证明对于 en-US、sv-SE、ko-KR 等语言Normal与Abbreviation的输出完全相同如1的序数词始终是first/första/첫번째因为它们的转换器不区分词形。culture 显式指定优先ToOrdinalWords_CanSpecifyCultureExplicitly验证了显式传new CultureInfo(ar)、ru等时输出为对应语言的序数词。多 locale 一致性测试目录 tests/Humanizer.Tests/Localisation 下按语言拆分维护了大量NumberToWordsTests.cs如pt-BR、de、fa等配合LocaleCoverageData等理论数据保证每个受支持 locale 的转换都被覆盖验证。小结与使用建议NumberToWordsExtension的每次调用都经Configurator.GetNumberToWordsConverter(culture)解析出与当前文化匹配的INumberToWordsConverter未注册的文化自动回退英语因此传null表示跟随当前线程 UI 文化传显式CultureInfo则强制指定语言。只有在词形/性别敏感的 locale西语、俄语、希伯来语、巴西葡语等中WordForm与GrammaticalGender参数才会改变输出英语等语言会静默忽略可放心传参。addAnd的默认值在不同重载间存在差异int版本默认truelong的WordForm版本默认false建议在关键业务代码中使用具名参数显式声明。序数词对1的处理存在母语捷径丹麦语、印尼语、马来语、斯洛伐克语、祖鲁语等直接返回første、pertama、prvý等形式需要精确控制时可直接传 culture 验证输出。如需深入了解转换器如何按 locale 组合如西语的性别规则、俄语的复数规则、印度的 lakh/crore 分组可继续阅读 src/Humanizer/Localisation/NumberToWords 下的各语言实现以及 Configurator 中的注册表设计。赞分享开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载相关推荐Humanizer 数字转英文/本地化单词完全指南NumberToWordsExtension 用法、重载与底层实现解析Humanizer 数字转英文/本地化单词完全指南NumberToWordsExtension 用法、重载与底层实现解析 本篇指南围绕 Humanizer 的开发工具Humanizer NumberToWordsExtension 全面指南在 .NET 中实现多语言数字与序数词的本地化转换Humanizer NumberToWordsExtension 全面指南在 .NET 中实现多语言数字与序数词的本地化转换 Humanizer 的 Numb开发工具Humanizer 数字转单词扩展深度指南NumberToWordsExtension 本地化基数词与序数词转换全解析Humanizer 数字转单词扩展深度指南NumberToWordsExtension 本地化基数词与序数词转换全解析 导读 Humanizer 的 Numb开发工具上一篇react-native-reanimated v1 multiply 节点声明式动画中的乘法节点用法与求值原理下一篇OpenZeppelin Contracts Upgradeable 变体自动生成全解析transpile 转译流水线与补丁管理机制创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考