百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
H

humanize

> 编程语言
开源

它会把你的数字变得*漂亮*。

473 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

它会把你的数字变得*漂亮*。

Humanize

Converts numbers to strings.

ruby
2.humanize  # => "two"
4.humanize  # => "four"
8.humanize  # => "eight"
15.humanize # => "fifteen"
16.humanize # => "sixteen"
23.humanize # => "twenty three"
42.humanize # => "forty two"

All the way up to 156 digit numbers:

…

If you are dealing with numbers larger than 156 digits, we accept patches. Word list is sourced from: [ Wordlist.source ]

Usage (Extends Ruby Core)

Note: this extends to Ruby core classes Integer, Float, and BigDecimal. See below for usage without extending Ruby core.

Install the gem using RubyGems

bash
gem install humanize

or:

Add it to your Gemfile

ruby
gem 'humanize'

Include it in your program

ruby
require 'humanize'

Call the method on the numbers

ruby
100.humanize => "one hundred"
1001.humanize => "one thousand and one"
0.001.humanize => "zero point zero zero one"

Usage (Without Extending Ruby Core)

Note: this usage does not extend Ruby core. Instead it relies on explicitly calling the pure function Humanize.format.

Install the gem using RubyGems

bash
gem install humanize

or:

Add it to your Gemfile

ruby
gem 'humanize', require: false

Include the module in your program

ruby
require 'humanize/module' # does not extend Ruby corex

Call the format function on the numbers

ruby
Humanize.format(100) => "one hundred"
Humanize.format(1001) => "one thousand and one"
Humanize.format(0.001) => "zero point zero zero one"

Configuration

ruby
Humanize.configure do |config|
  config.locale = :en  # [:en, :es, :fr, :'fr-CH', :tr, :az, :de, :id, :th, :ru, :pt, :ms, :jp, :vi, :'zh-tw', :ar], default: :en
  config.decimals_as = :digits # [:digits, :number], default: :digits
end

The default locale can be overridden by passing an explicit locale::

ruby
42.humanize(locale: :fr) # => "quarante-deux"
1666.humanize(locale: :tr) # => "bin altı yüz altmış altı"

Or, functionally:

ruby
Humanize.format(42, locale: :fr) # => "quarante-deux"
Humanize.format(1666, locale: :tr) # => "bin altı yüz altmış altı"

Decimals

Similarly, the default display of decimals can be overridden by passing an explicit decimals_as: value:

ruby
0.42.humanize(decimals_as: :digits) # => "zero point four two"
0.42.humanize(decimals_as: :number) # => "zero point forty-two"

Or, functionally:

ruby
Humanize.format(0.42, decimals_as: :digits) # => "zero point four two"
Humanize.format(0.42, decimals_as: :number) # => "zero point forty-two"

I18n

Currently supported locales:

  • English: :en
  • Spanish: :es
  • French: :fr
  • Swiss French: :'fr-CH'
  • Turkish: :tr
  • Azerbaijani :az
  • German: :de
  • Indonesian: :id
  • Thai: :th
  • Russian: :ru (except for decimals)
  • Portuguese: :pt
  • Malaysia :ms
  • Japanese :jp
  • Vietnamese :vi
  • Traditional Chinese :zh-tw
  • Arabic :ar

Testing

Install development dependencies by running bundle install.

You can run mutation testing by calling bin/run_mutant.

Credits

  • Original idea: Brenton Fletcher
  • Unbastardisation of original idea: Ryan Bigg
  • Awesome speed up: Jack "Chendo" Chen

Issues· 8 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言