num2words 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +40 -0
- data/CHANGELOG.md +39 -0
- data/Gemfile +1 -0
- data/README.md +83 -0
- data/Rakefile +38 -2
- data/benchmark/converter_benchmark.rb +96 -0
- data/docs/api.md +478 -0
- data/docs/api.ru.md +478 -0
- data/docs/limits.md +140 -0
- data/docs/limits.ru.md +140 -0
- data/docs/locale_development.md +241 -0
- data/docs/locale_development.ru.md +241 -0
- data/docs/rails.md +130 -0
- data/docs/rails.ru.md +130 -0
- data/lib/num2words/config.rb +44 -0
- data/lib/num2words/converter.rb +2 -1
- data/lib/num2words/rails/helpers.rb +27 -0
- data/lib/num2words/rails/validators/currency_validator.rb +30 -0
- data/lib/num2words/rails/validators/locale_validator.rb +22 -0
- data/lib/num2words/railtie.rb +27 -0
- data/lib/num2words/version.rb +1 -1
- data/lib/num2words.rb +2 -0
- data/num2words.gemspec +1 -0
- metadata +30 -2
data/docs/api.md
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
This document describes the public API of `num2words`.
|
|
4
|
+
|
|
5
|
+
## Loading
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
require "num2words"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This loads the module API and core extensions for supported Ruby classes.
|
|
12
|
+
|
|
13
|
+
## Module API
|
|
14
|
+
|
|
15
|
+
### `Num2words.to_words`
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
Num2words.to_words(value, locale = I18n.default_locale, only = nil, short = false, **options)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Converts numbers, dates, times and date-times to words.
|
|
22
|
+
|
|
23
|
+
Examples:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
Num2words.to_words(123, :ru)
|
|
27
|
+
# => "сто двадцать три"
|
|
28
|
+
|
|
29
|
+
Num2words.to_words("3,5", :ru)
|
|
30
|
+
# => "три целых пять десятых"
|
|
31
|
+
|
|
32
|
+
Num2words.to_words("2024-08-21", :en)
|
|
33
|
+
# => "the twenty-first of August, two thousand twenty four"
|
|
34
|
+
|
|
35
|
+
Num2words.to_words("14:35:42", :fr)
|
|
36
|
+
# => "quatorze heures trente cinq minutes quarante deux secondes"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Keyword locale form is also supported:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
Num2words.to_words(123, locale: :en)
|
|
43
|
+
# => "one hundred twenty three"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### `Num2words.to_currency`
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
Num2words.to_currency(amount, locale = I18n.default_locale, **options)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Converts a numeric amount to words using the selected locale and currency.
|
|
53
|
+
|
|
54
|
+
Examples:
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
Num2words.to_currency("21,05", :ru)
|
|
58
|
+
# => "двадцать один рубль пять копеек"
|
|
59
|
+
|
|
60
|
+
Num2words.to_currency(12.50, :en, code: :EUR)
|
|
61
|
+
# => "twelve euros fifty cents"
|
|
62
|
+
|
|
63
|
+
Num2words.to_currency(BigDecimal("21.05"), :ru)
|
|
64
|
+
# => "двадцать один рубль пять копеек"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Core Extensions
|
|
68
|
+
|
|
69
|
+
`num2words` adds convenience methods to common classes.
|
|
70
|
+
|
|
71
|
+
### `Integer#to_words`
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
123.to_words(:ru)
|
|
75
|
+
# => "сто двадцать три"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `Integer#to_currency`
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
123.to_currency(:en, code: :USD)
|
|
82
|
+
# => "one hundred twenty three dollars zero cents"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### `Float#to_words`
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
45.67.to_words(:ru)
|
|
89
|
+
# => "сорок пять целых шестьдесят семь сотых"
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### `Float#to_currency`
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
12.5.to_currency(:ru, code: :USD)
|
|
96
|
+
# => "двенадцать долларов пятьдесят центов"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### `String#to_words`
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
"007".to_words(:en)
|
|
103
|
+
# => "seven"
|
|
104
|
+
|
|
105
|
+
"3,5".to_words(:ru)
|
|
106
|
+
# => "три целых пять десятых"
|
|
107
|
+
|
|
108
|
+
"2024-08-21 14:35:42".to_words(:ru)
|
|
109
|
+
# => "двадцать первое августа две тысячи двадцать четвёртого года, четырнадцать часов тридцать пять минут сорок две секунды"
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`String#to_currency` is not currently defined. Use `Num2words.to_currency("12.50", :en)` for string amounts.
|
|
113
|
+
|
|
114
|
+
### `Date#to_words`
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
Date.new(2024, 8, 21).to_words(:ru)
|
|
118
|
+
# => "двадцать первое августа две тысячи двадцать четвёртого года"
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### `Time#to_words`
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
Time.new(2024, 8, 21, 14, 35, 42).to_words(:en)
|
|
125
|
+
# => "fourteen hours thirty five minutes forty two seconds"
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### `DateTime#to_words`
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
DateTime.parse("2024-08-21 14:35:42").to_words(:en)
|
|
132
|
+
# => "the twenty-first of August, two thousand twenty four at fourteen hours thirty five minutes forty two seconds"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Supported Input Types
|
|
136
|
+
|
|
137
|
+
`to_words` supports:
|
|
138
|
+
|
|
139
|
+
- `Integer`
|
|
140
|
+
- `Float`
|
|
141
|
+
- `Date`
|
|
142
|
+
- `Time`
|
|
143
|
+
- `DateTime`
|
|
144
|
+
- integer strings, for example `"007"` or `"-42"`
|
|
145
|
+
- decimal strings with dot or comma, for example `"3.5"` or `"3,5"`
|
|
146
|
+
- date strings, for example `"2024-08-21"` or `"21.08.2024"`
|
|
147
|
+
- time strings, for example `"14:35"` or `"14:35:42"`
|
|
148
|
+
- date-time strings, for example `"2024-08-21 14:35:42"`
|
|
149
|
+
|
|
150
|
+
`to_currency` supports:
|
|
151
|
+
|
|
152
|
+
- `Integer`
|
|
153
|
+
- `Float`
|
|
154
|
+
- `String`
|
|
155
|
+
- `BigDecimal`
|
|
156
|
+
|
|
157
|
+
String currency amounts may use dot or comma as decimal separator.
|
|
158
|
+
|
|
159
|
+
## Options
|
|
160
|
+
|
|
161
|
+
### `locale`
|
|
162
|
+
|
|
163
|
+
Selects the locale.
|
|
164
|
+
|
|
165
|
+
```ruby
|
|
166
|
+
Num2words.to_words(123, :ru)
|
|
167
|
+
Num2words.to_words(123, locale: :ru)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
If no locale is passed, `I18n.default_locale` is used.
|
|
171
|
+
|
|
172
|
+
### `style`
|
|
173
|
+
|
|
174
|
+
Controls fractional output in `to_words`.
|
|
175
|
+
|
|
176
|
+
Supported values:
|
|
177
|
+
|
|
178
|
+
- `:fraction` - default fraction style.
|
|
179
|
+
- `:decimal` - reads fractional digits one by one when the locale supports decimal digit style.
|
|
180
|
+
|
|
181
|
+
```ruby
|
|
182
|
+
Num2words.to_words(12.12, :en)
|
|
183
|
+
# => "twelve and twelve hundredths"
|
|
184
|
+
|
|
185
|
+
Num2words.to_words(12.12, :en, style: :decimal)
|
|
186
|
+
# => "twelve point one two"
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### `joiner`
|
|
190
|
+
|
|
191
|
+
Controls the connector for fraction style.
|
|
192
|
+
|
|
193
|
+
Supported values:
|
|
194
|
+
|
|
195
|
+
- `:default`
|
|
196
|
+
- `:and`
|
|
197
|
+
|
|
198
|
+
```ruby
|
|
199
|
+
Num2words.to_words(0.5, :ru)
|
|
200
|
+
# => "ноль целых пять десятых"
|
|
201
|
+
|
|
202
|
+
Num2words.to_words(0.5, :ru, joiner: :and)
|
|
203
|
+
# => "ноль и пять десятых"
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Invalid values raise `ArgumentError`.
|
|
207
|
+
|
|
208
|
+
### `date_case`
|
|
209
|
+
|
|
210
|
+
Controls date day case for locales that support it.
|
|
211
|
+
|
|
212
|
+
Supported values:
|
|
213
|
+
|
|
214
|
+
- `:default`
|
|
215
|
+
- `:genitive`
|
|
216
|
+
|
|
217
|
+
```ruby
|
|
218
|
+
Num2words.to_words("2024-08-21", :ru)
|
|
219
|
+
# => "двадцать первое августа две тысячи двадцать четвёртого года"
|
|
220
|
+
|
|
221
|
+
Num2words.to_words("2024-08-21", :ru, date_case: :genitive)
|
|
222
|
+
# => "двадцать первого августа две тысячи двадцать четвёртого года"
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Invalid values raise `ArgumentError`.
|
|
226
|
+
|
|
227
|
+
### `short`
|
|
228
|
+
|
|
229
|
+
Enables short output for time or date-time values.
|
|
230
|
+
|
|
231
|
+
```ruby
|
|
232
|
+
Num2words.to_words("14:35:42", :ru, short: true)
|
|
233
|
+
# => "четырнадцать часов тридцать пять минут"
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
With date-time values, `short: true` returns a shortened date-time string unless `only` is used.
|
|
237
|
+
|
|
238
|
+
### `only`
|
|
239
|
+
|
|
240
|
+
Selects a part of a date-time value.
|
|
241
|
+
|
|
242
|
+
```ruby
|
|
243
|
+
Num2words.to_words("2024-08-21 14:35:42", :ru, only: :date)
|
|
244
|
+
# => "двадцать первое августа две тысячи двадцать четвёртого года"
|
|
245
|
+
|
|
246
|
+
Num2words.to_words("2024-08-21 14:35:42", :ru, only: :time)
|
|
247
|
+
# => "четырнадцать часов тридцать пять минут сорок две секунды"
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
The legacy positional form is also supported:
|
|
251
|
+
|
|
252
|
+
```ruby
|
|
253
|
+
"2024-08-21 14:35:42".to_words(:ru, :date)
|
|
254
|
+
"2024-08-21 14:35:42".to_words(:ru, :time)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### `format`
|
|
258
|
+
|
|
259
|
+
Used by date and time conversion.
|
|
260
|
+
|
|
261
|
+
For dates:
|
|
262
|
+
|
|
263
|
+
- `:default`
|
|
264
|
+
- `:nominative` when provided by locale data.
|
|
265
|
+
- `:short` returns numeric date format `DD.MM.YYYY`.
|
|
266
|
+
|
|
267
|
+
For times:
|
|
268
|
+
|
|
269
|
+
- `:default`
|
|
270
|
+
- `:hours_only`
|
|
271
|
+
- `:hours_minutes`
|
|
272
|
+
- `:hours_minutes_seconds`
|
|
273
|
+
|
|
274
|
+
```ruby
|
|
275
|
+
Num2words.to_words("14:35:42", :ru, format: :hours_minutes)
|
|
276
|
+
# => "четырнадцать часов тридцать пять минут"
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Unsupported time formats raise `ArgumentError`.
|
|
280
|
+
|
|
281
|
+
### `word_case`
|
|
282
|
+
|
|
283
|
+
Changes result casing.
|
|
284
|
+
|
|
285
|
+
Supported values:
|
|
286
|
+
|
|
287
|
+
- `:default`
|
|
288
|
+
- `:upper`
|
|
289
|
+
- `:downcase`
|
|
290
|
+
- `:capitalize`
|
|
291
|
+
- `:title`
|
|
292
|
+
|
|
293
|
+
```ruby
|
|
294
|
+
Num2words.to_words(21, :en, word_case: :upper)
|
|
295
|
+
# => "TWENTY ONE"
|
|
296
|
+
|
|
297
|
+
Num2words.to_words(21, :en, word_case: :title)
|
|
298
|
+
# => "Twenty One"
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### `feminine`
|
|
302
|
+
|
|
303
|
+
For locales with grammatical gender, selects feminine forms where supported.
|
|
304
|
+
|
|
305
|
+
```ruby
|
|
306
|
+
Num2words.to_words(1, :ru, feminine: true)
|
|
307
|
+
# => "одна"
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### `code`
|
|
311
|
+
|
|
312
|
+
Selects currency code in `to_currency`.
|
|
313
|
+
|
|
314
|
+
```ruby
|
|
315
|
+
Num2words.to_currency(12.50, :ru, code: :USD)
|
|
316
|
+
# => "двенадцать долларов пятьдесят центов"
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
If the currency is not available for the locale, `num2words` falls back to the locale default currency and may emit a warning depending on configuration.
|
|
320
|
+
|
|
321
|
+
### `minor`
|
|
322
|
+
|
|
323
|
+
Controls minor currency unit output.
|
|
324
|
+
|
|
325
|
+
Supported values:
|
|
326
|
+
|
|
327
|
+
- `:always` - always include minor units.
|
|
328
|
+
- `:nonzero` - include minor units only when the minor value is greater than zero.
|
|
329
|
+
- `:never` - do not include minor units.
|
|
330
|
+
|
|
331
|
+
```ruby
|
|
332
|
+
Num2words.to_currency(12, :ru)
|
|
333
|
+
# => "двенадцать рублей ноль копеек"
|
|
334
|
+
|
|
335
|
+
Num2words.to_currency(12, :ru, minor: :nonzero)
|
|
336
|
+
# => "двенадцать рублей"
|
|
337
|
+
|
|
338
|
+
Num2words.to_currency(12.50, :ru, minor: :never)
|
|
339
|
+
# => "двенадцать рублей"
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Invalid values raise `ArgumentError`.
|
|
343
|
+
|
|
344
|
+
## Configuration
|
|
345
|
+
|
|
346
|
+
### `Num2words.default_currency`
|
|
347
|
+
|
|
348
|
+
Gets or sets default currency.
|
|
349
|
+
|
|
350
|
+
```ruby
|
|
351
|
+
Num2words.default_currency(:ru)
|
|
352
|
+
# => :RUB
|
|
353
|
+
|
|
354
|
+
Num2words.default_currency(:ru, :USD)
|
|
355
|
+
# => :USD
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### `Num2words.available_currencies`
|
|
359
|
+
|
|
360
|
+
Returns currency codes available for a locale.
|
|
361
|
+
|
|
362
|
+
```ruby
|
|
363
|
+
Num2words.available_currencies(:ru)
|
|
364
|
+
# => [:RUB, :USD, :EUR, ...]
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### `Num2words.currency_available?`
|
|
368
|
+
|
|
369
|
+
Checks whether a currency is available for a locale.
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
Num2words.currency_available?(:ru, :RUB)
|
|
373
|
+
# => true
|
|
374
|
+
|
|
375
|
+
Num2words.currency_available?(:ru, "usd")
|
|
376
|
+
# => true
|
|
377
|
+
|
|
378
|
+
Num2words.currency_available?(:ru, :XYZ)
|
|
379
|
+
# => false
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### `Num2words.currency_info`
|
|
383
|
+
|
|
384
|
+
Returns currency metadata for a locale or `nil` when the currency is unavailable.
|
|
385
|
+
|
|
386
|
+
```ruby
|
|
387
|
+
Num2words.currency_info(:ru, :RUB)
|
|
388
|
+
# => {
|
|
389
|
+
# code: :RUB,
|
|
390
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
391
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
392
|
+
# symbol: "₽"
|
|
393
|
+
# }
|
|
394
|
+
|
|
395
|
+
Num2words.currency_info(:ru, :XYZ)
|
|
396
|
+
# => nil
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### `Num2words.default_currency_info`
|
|
400
|
+
|
|
401
|
+
Returns metadata for the locale default currency.
|
|
402
|
+
|
|
403
|
+
```ruby
|
|
404
|
+
Num2words.default_currency_info(:ru)
|
|
405
|
+
# => {
|
|
406
|
+
# code: :RUB,
|
|
407
|
+
# major_unit: ["рубль", "рубля", "рублей"],
|
|
408
|
+
# minor_unit: ["копейка", "копейки", "копеек"],
|
|
409
|
+
# symbol: "₽"
|
|
410
|
+
# }
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### `Num2words.available_locales`
|
|
414
|
+
|
|
415
|
+
Returns supported locale codes.
|
|
416
|
+
|
|
417
|
+
```ruby
|
|
418
|
+
Num2words.available_locales
|
|
419
|
+
# => [:ar, :be, :bg, :bn, :cs, ...]
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
### `Num2words.currency_warnings`
|
|
423
|
+
|
|
424
|
+
Controls warnings when requested currency is unavailable.
|
|
425
|
+
|
|
426
|
+
```ruby
|
|
427
|
+
Num2words.currency_warnings = false
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
## Locales and Currencies
|
|
431
|
+
|
|
432
|
+
Supported locales:
|
|
433
|
+
|
|
434
|
+
```text
|
|
435
|
+
ar be bg bn cs da de el en es et fa fi fr gu he hi hr hu id it ja kn ko
|
|
436
|
+
kz lt lv ml mr ms nb nl pa pl pt ro ru sk sl sr sv sw ta te th tr uk ur
|
|
437
|
+
vi zh
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
Each completed locale supports the same 32 currency codes:
|
|
441
|
+
|
|
442
|
+
```text
|
|
443
|
+
BDT BGN BRL BYN CNY CZK DKK EUR GBP HUF IDR ILS INR IRR JPY KES KRW
|
|
444
|
+
KZT MYR NOK PKR PLN RON RSD RUB SAR SEK THB TRY UAH USD VND
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## Errors
|
|
448
|
+
|
|
449
|
+
Unsupported input type:
|
|
450
|
+
|
|
451
|
+
```ruby
|
|
452
|
+
Num2words.to_words(Object.new, :en)
|
|
453
|
+
# raises ArgumentError: Unsupported input type: ...
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
Unsupported options:
|
|
457
|
+
|
|
458
|
+
```ruby
|
|
459
|
+
Num2words.to_words(0.5, :ru, joiner: :plus)
|
|
460
|
+
# raises ArgumentError: Unsupported joiner option: :plus
|
|
461
|
+
|
|
462
|
+
Num2words.to_currency(12, :ru, minor: :sometimes)
|
|
463
|
+
# raises ArgumentError: Unsupported minor option: :sometimes
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
Unsupported currency amount:
|
|
467
|
+
|
|
468
|
+
```ruby
|
|
469
|
+
Num2words.to_currency("abc", :en)
|
|
470
|
+
# raises ArgumentError: Unsupported currency amount: "abc"
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
## Development References
|
|
474
|
+
|
|
475
|
+
- [Numeric limits](limits.md)
|
|
476
|
+
- [Rails integration](rails.md)
|
|
477
|
+
- [Locale development guide](locale_development.md)
|
|
478
|
+
- [Russian locale development guide](locale_development.ru.md)
|