chislo 0.3.1

Convert numbers to Russian words (числа прописью) with grammatical gender, noun declension, ordinals, currency, percentages, durations and dates. Supports i64 integers (quintillions) and decimals with configurable precision.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#[cfg(not(feature = "std"))]
use alloc::{format, string::String};

use crate::Gender;
use crate::convert::convert_int_to_words;
use crate::decline::get_declension;
use crate::parse::{parse_fractional_digits, split_decimal, strip_sign};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Currency<'a> {
    pub whole: (&'a str, &'a str, &'a str),
    pub whole_gender: Gender,
    pub frac: (&'a str, &'a str, &'a str),
    pub frac_gender: Gender,
    pub show_frac: bool,
}

impl<'a> Currency<'a> {
    pub const fn new(
        whole: (&'a str, &'a str, &'a str),
        whole_gender: Gender,
        frac: (&'a str, &'a str, &'a str),
        frac_gender: Gender,
    ) -> Self {
        Self {
            whole,
            whole_gender,
            frac,
            frac_gender,
            show_frac: true,
        }
    }

    pub fn from_iso(code: &str) -> Option<&'static Currency<'static>> {
        let bytes = code.as_bytes();
        if bytes.len() != 3 {
            return None;
        }

        let upper = [
            bytes[0].to_ascii_uppercase(),
            bytes[1].to_ascii_uppercase(),
            bytes[2].to_ascii_uppercase(),
        ];

        for &(iso, cur) in ISO_CURRENCIES {
            if iso.as_bytes() == upper {
                return Some(cur);
            }
        }
        None
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum RoundingMode {
    #[default]
    Trunc,
    HalfUp,
    HalfEven,
}

pub const RUB: Currency<'static> = Currency {
    whole: ("рубль", "рубля", "рублей"),
    whole_gender: Gender::Masculine,
    frac: ("копейка", "копейки", "копеек"),
    frac_gender: Gender::Feminine,
    show_frac: true,
};

pub const USD: Currency<'static> = Currency {
    whole: ("доллар", "доллара", "долларов"),
    whole_gender: Gender::Masculine,
    frac: ("цент", "цента", "центов"),
    frac_gender: Gender::Masculine,
    show_frac: true,
};

pub const EUR: Currency<'static> = Currency {
    whole: ("евро", "евро", "евро"),
    whole_gender: Gender::Neuter,
    frac: ("цент", "цента", "центов"),
    frac_gender: Gender::Masculine,
    show_frac: true,
};

pub const GBP: Currency<'static> = Currency {
    whole: ("фунт стерлингов", "фунта стерлингов", "фунтов стерлингов"),
    whole_gender: Gender::Masculine,
    frac: ("пенс", "пенса", "пенсов"),
    frac_gender: Gender::Masculine,
    show_frac: true,
};

pub const CNY: Currency<'static> = Currency {
    whole: ("юань", "юаня", "юаней"),
    whole_gender: Gender::Masculine,
    frac: ("фэнь", "фэня", "фэней"),
    frac_gender: Gender::Masculine,
    show_frac: true,
};

pub const JPY: Currency<'static> = Currency {
    whole: ("иена", "иены", "иен"),
    whole_gender: Gender::Feminine,
    frac: ("сен", "сена", "сенов"),
    frac_gender: Gender::Masculine,
    show_frac: false,
};

pub const KZT: Currency<'static> = Currency {
    whole: ("тенге", "тенге", "тенге"),
    whole_gender: Gender::Masculine,
    frac: ("тиын", "тиына", "тиынов"),
    frac_gender: Gender::Masculine,
    show_frac: true,
};

pub const BYN: Currency<'static> = Currency {
    whole: (
        "белорусский рубль",
        "белорусских рубля",
        "белорусских рублей",
    ),
    whole_gender: Gender::Masculine,
    frac: ("копейка", "копейки", "копеек"),
    frac_gender: Gender::Feminine,
    show_frac: true,
};

pub const UAH: Currency<'static> = Currency {
    whole: ("гривна", "гривны", "гривен"),
    whole_gender: Gender::Feminine,
    frac: ("копейка", "копейки", "копеек"),
    frac_gender: Gender::Feminine,
    show_frac: true,
};

pub const CHF: Currency<'static> = Currency {
    whole: (
        "швейцарский франк",
        "швейцарских франка",
        "швейцарских франков",
    ),
    whole_gender: Gender::Masculine,
    frac: ("сантим", "сантима", "сантимов"),
    frac_gender: Gender::Masculine,
    show_frac: true,
};

pub const AED: Currency<'static> = Currency {
    whole: ("дирхам", "дирхама", "дирхамов"),
    whole_gender: Gender::Masculine,
    frac: ("филс", "филса", "филсов"),
    frac_gender: Gender::Masculine,
    show_frac: true,
};

const ISO_CURRENCIES: &[(&str, &Currency<'static>)] = &[
    ("RUB", &RUB),
    ("USD", &USD),
    ("EUR", &EUR),
    ("GBP", &GBP),
    ("CNY", &CNY),
    ("JPY", &JPY),
    ("KZT", &KZT),
    ("BYN", &BYN),
    ("UAH", &UAH),
    ("CHF", &CHF),
    ("AED", &AED),
];

pub fn money(whole: i64, cents: u32, currency: &Currency) -> String {
    let whole_words = convert_int_to_words(whole, currency.whole_gender);
    let whole_decl = get_declension(whole, currency.whole.0, currency.whole.1, currency.whole.2);

    if !currency.show_frac {
        return format!("{whole_words} {whole_decl}");
    }

    let cents_words = convert_int_to_words(cents as i64, currency.frac_gender);
    let cents_decl = get_declension(
        cents as i64,
        currency.frac.0,
        currency.frac.1,
        currency.frac.2,
    );
    format!("{whole_words} {whole_decl} {cents_words} {cents_decl}")
}

pub fn money_from_str(amount: &str, currency: &Currency) -> Result<String, crate::Error> {
    money_from_str_rounded(amount, currency, RoundingMode::Trunc)
}

pub fn money_from_str_rounded(
    amount: &str,
    currency: &Currency,
    mode: RoundingMode,
) -> Result<String, crate::Error> {
    let (negative, rest) = strip_sign(amount);
    let (whole_str, frac_opt) = split_decimal(rest);
    let mut whole: i64 = if negative {
        format!("-{whole_str}").parse()
    } else {
        whole_str.parse()
    }
    .map_err(|_| crate::Error::InvalidNumber(format!("invalid amount: '{whole_str}'")))?;
    let (cents, carry) = round_cents(frac_opt.unwrap_or(""), mode)?;

    if carry {
        if negative {
            whole = whole.checked_sub(1).ok_or(crate::Error::NumberTooLarge)?;
        } else {
            whole = whole.checked_add(1).ok_or(crate::Error::NumberTooLarge)?;
        }
    }

    let result = money(whole, cents, currency);
    if negative && whole == 0 {
        Ok(format!("минус {result}"))
    } else {
        Ok(result)
    }
}

fn round_cents(frac_str: &str, mode: RoundingMode) -> Result<(u32, bool), crate::Error> {
    if frac_str.is_empty() || matches!(mode, RoundingMode::Trunc) {
        return Ok((parse_fractional_digits(frac_str, 2)?, false));
    }

    let to_digit = |c: char| {
        c.to_digit(10).ok_or_else(|| {
            crate::Error::InvalidNumber(format!("invalid fractional part: '{frac_str}'"))
        })
    };

    let mut chars = frac_str.chars();
    let d1 = match chars.next() {
        Some(c) => to_digit(c)?,
        None => return Ok((parse_fractional_digits(frac_str, 2)?, false)),
    };
    let d2 = match chars.next() {
        Some(c) => to_digit(c)?,
        None => return Ok((parse_fractional_digits(frac_str, 2)?, false)),
    };
    let d3 = match chars.next() {
        Some(c) => to_digit(c)?,
        None => return Ok((parse_fractional_digits(frac_str, 2)?, false)),
    };
    let mut tail_nonzero = false;
    for c in chars {
        to_digit(c)?;
        if c != '0' {
            tail_nonzero = true;
        }
    }
    let mut cents = d1 * 10 + d2;

    let round_up = match mode {
        RoundingMode::Trunc => false,
        RoundingMode::HalfUp => d3 >= 5,
        RoundingMode::HalfEven => match d3.cmp(&5) {
            core::cmp::Ordering::Greater => true,
            core::cmp::Ordering::Less => false,
            core::cmp::Ordering::Equal => tail_nonzero || cents % 2 == 1,
        },
    };

    if round_up {
        cents += 1;
    }

    if cents >= 100 {
        Ok((cents - 100, true))
    } else {
        Ok((cents, false))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_money_rub() {
        assert_eq!(
            money(1234, 56, &RUB),
            "одна тысяча двести тридцать четыре рубля пятьдесят шесть копеек"
        );
        assert_eq!(money(1, 1, &RUB), "один рубль одна копейка");
    }

    #[test]
    fn test_money_from_str() {
        assert_eq!(
            money_from_str("1234.56", &RUB).unwrap(),
            "одна тысяча двести тридцать четыре рубля пятьдесят шесть копеек"
        );
        assert!(money_from_str("abc", &RUB).is_err());
    }

    #[test]
    fn test_money_from_str_comma() {
        assert_eq!(
            money_from_str("1234,56", &RUB).unwrap(),
            "одна тысяча двести тридцать четыре рубля пятьдесят шесть копеек"
        );
    }

    #[test]
    fn test_money_all_currencies() {
        let cases: &[(i64, u32, &Currency, &str)] = &[
            (
                1234,
                56,
                &RUB,
                "одна тысяча двести тридцать четыре рубля пятьдесят шесть копеек",
            ),
            (
                1234,
                56,
                &USD,
                "одна тысяча двести тридцать четыре доллара пятьдесят шесть центов",
            ),
            (
                1234,
                56,
                &EUR,
                "одна тысяча двести тридцать четыре евро пятьдесят шесть центов",
            ),
            (1, 0, &EUR, "одно евро ноль центов"),
        ];
        for &(whole, cents, currency, expected) in cases {
            assert_eq!(
                money(whole, cents, currency),
                expected,
                "money({whole}, {cents})"
            );
        }
    }

    #[test]
    fn test_money_new_currencies() {
        assert_eq!(
            money(10, 50, &GBP),
            "десять фунтов стерлингов пятьдесят пенсов"
        );
        assert_eq!(money(3, 25, &CNY), "три юаня двадцать пять фэней");
        assert_eq!(money(100, 0, &JPY), "сто иен");
        assert_eq!(money(1, 0, &JPY), "одна иена");
        assert_eq!(money(500, 0, &KZT), "пятьсот тенге ноль тиынов");
        assert_eq!(money(1, 0, &UAH), "одна гривна ноль копеек");
    }

    #[test]
    fn test_currency_from_iso() {
        assert!(Currency::from_iso("RUB").is_some());
        assert!(Currency::from_iso("rub").is_some());
        assert!(Currency::from_iso("JPY").is_some());
        assert!(Currency::from_iso("XYZ").is_none());
        assert!(Currency::from_iso("RU").is_none());
        assert!(Currency::from_iso("RUBL").is_none());
        let cur = Currency::from_iso("USD").unwrap();
        assert_eq!(money(1, 0, cur), "один доллар ноль центов");
    }

    #[test]
    fn test_money_declension_boundaries() {
        let cases: &[(i64, u32, &Currency, &str)] = &[
            (0, 0, &RUB, "ноль рублей ноль копеек"),
            (1, 1, &RUB, "один рубль одна копейка"),
            (2, 2, &RUB, "два рубля две копейки"),
            (5, 5, &RUB, "пять рублей пять копеек"),
            (11, 11, &RUB, "одиннадцать рублей одиннадцать копеек"),
            (21, 21, &RUB, "двадцать один рубль двадцать одна копейка"),
            (22, 22, &RUB, "двадцать два рубля двадцать две копейки"),
            (1, 1, &USD, "один доллар один цент"),
            (2, 2, &USD, "два доллара два цента"),
            (5, 5, &USD, "пять долларов пять центов"),
            (21, 21, &USD, "двадцать один доллар двадцать один цент"),
        ];
        for &(whole, cents, currency, expected) in cases {
            assert_eq!(
                money(whole, cents, currency),
                expected,
                "declension({whole}, {cents})"
            );
        }
    }

    #[test]
    fn test_money_large_amounts() {
        assert_eq!(money(1_000_000, 0, &RUB), "один миллион рублей ноль копеек");
        assert_eq!(
            money(999_999, 99, &RUB),
            "девятьсот девяносто девять тысяч девятьсот девяносто девять рублей девяносто девять копеек"
        );
    }

    #[test]
    fn test_money_from_str_edge_cases() {
        let ok_cases: &[(&str, &Currency, &str)] = &[
            ("100", &RUB, "сто рублей ноль копеек"),
            ("100.5", &RUB, "сто рублей пятьдесят копеек"),
            ("100.05", &RUB, "сто рублей пять копеек"),
            ("100.999", &RUB, "сто рублей девяносто девять копеек"),
            ("0.00", &RUB, "ноль рублей ноль копеек"),
            ("1.01", &USD, "один доллар один цент"),
            ("1.01", &EUR, "одно евро один цент"),
        ];
        for &(input, currency, expected) in ok_cases {
            assert_eq!(
                money_from_str(input, currency).unwrap(),
                expected,
                "money_from_str(\"{input}\")"
            );
        }
        assert!(money_from_str("abc", &RUB).is_err());
        assert!(money_from_str("", &RUB).is_err());
        assert!(
            money_from_str_rounded("1.125x", &RUB, RoundingMode::HalfUp).is_err(),
            "non-digit characters in fractional tail must be rejected"
        );
    }

    #[test]
    fn test_money_rounding_half_up() {
        assert_eq!(
            money_from_str_rounded("100.994", &RUB, RoundingMode::HalfUp).unwrap(),
            "сто рублей девяносто девять копеек"
        );
        assert_eq!(
            money_from_str_rounded("100.995", &RUB, RoundingMode::HalfUp).unwrap(),
            "сто один рубль ноль копеек"
        );
        assert_eq!(
            money_from_str_rounded("100.999", &RUB, RoundingMode::HalfUp).unwrap(),
            "сто один рубль ноль копеек"
        );
    }

    #[test]
    fn test_money_rounding_half_even() {
        assert_eq!(
            money_from_str_rounded("1.125", &RUB, RoundingMode::HalfEven).unwrap(),
            "один рубль двенадцать копеек"
        );
        assert_eq!(
            money_from_str_rounded("1.135", &RUB, RoundingMode::HalfEven).unwrap(),
            "один рубль четырнадцать копеек"
        );
        assert_eq!(
            money_from_str_rounded("1.1251", &RUB, RoundingMode::HalfEven).unwrap(),
            "один рубль тринадцать копеек"
        );
        // non-zero digit beyond 18th position must still break the tie
        assert_eq!(
            money_from_str_rounded("1.1250000000000000001", &RUB, RoundingMode::HalfEven).unwrap(),
            "один рубль тринадцать копеек"
        );
    }

    #[test]
    fn test_money_from_str_negative() {
        assert_eq!(
            money_from_str("-1234.56", &RUB).unwrap(),
            "минус одна тысяча двести тридцать четыре рубля пятьдесят шесть копеек"
        );
        assert_eq!(
            money_from_str("-0.50", &RUB).unwrap(),
            "минус ноль рублей пятьдесят копеек"
        );
        assert_eq!(
            money_from_str("-0.01", &RUB).unwrap(),
            "минус ноль рублей одна копейка"
        );
        assert_eq!(
            money_from_str("-5", &USD).unwrap(),
            "минус пять долларов ноль центов"
        );
    }

    #[test]
    fn test_money_rounding_negative_subunit() {
        assert_eq!(
            money_from_str_rounded("-0.995", &RUB, RoundingMode::HalfUp).unwrap(),
            "минус один рубль ноль копеек"
        );
        assert_eq!(
            money_from_str_rounded("-0.995", &RUB, RoundingMode::HalfEven).unwrap(),
            "минус один рубль ноль копеек"
        );
        assert_eq!(
            money_from_str_rounded("-0.125", &RUB, RoundingMode::HalfEven).unwrap(),
            "минус ноль рублей двенадцать копеек"
        );
    }

    #[test]
    fn test_money_from_str_i64_min_boundary() {
        assert!(money_from_str("-9223372036854775808.01", &RUB).is_ok());
        assert_eq!(
            money_from_str("-9223372036854775808.01", &RUB).unwrap(),
            "минус девять квинтиллионов двести двадцать три квадриллиона триста семьдесят два триллиона тридцать шесть миллиардов восемьсот пятьдесят четыре миллиона семьсот семьдесят пять тысяч восемьсот восемь рублей одна копейка"
        );
    }

    #[test]
    fn test_money_custom_currency() {
        let custom = Currency::new(
            ("тонна", "тонны", "тонн"),
            Gender::Feminine,
            ("килограмм", "килограмма", "килограммов"),
            Gender::Masculine,
        );
        assert_eq!(money(1, 0, &custom), "одна тонна ноль килограммов");
        assert_eq!(money(2, 1, &custom), "две тонны один килограмм");
        assert_eq!(money(5, 5, &custom), "пять тонн пять килограммов");
    }
}