n2words 0.1.1

Convert numbers to words in 50 languages (72 regional variants): cardinal, ordinal and currency forms, with arbitrary-precision input
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Numeric input and the parsers each form uses.
//!
//! Every conversion function takes `impl Into<Number>`, so integers, floats,
//! numeric strings and big integers are all accepted. Strings keep arbitrary
//! precision (`"12345678901234567890"` is exact) and accept scientific
//! notation; floats go through their shortest round-trip representation, so
//! `0.1` spells "zero point one", not its binary expansion.

use crate::error::{Error, Result};
use num_bigint::{BigInt, BigUint, Sign};
use num_traits::{ToPrimitive, Zero};
use std::borrow::Cow;

/// A numeric value accepted by every form.
///
/// Build one through the `From` conversions rather than directly:
/// `to_cardinal(42)`, `to_cardinal("42.5")`, `to_cardinal(1e21)`,
/// `to_cardinal(BigInt::from(7))` all work.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Number<'a> {
    /// Any primitive integer except `u128`.
    Int(i128),
    /// A finite `f64`, spelled from its shortest round-trip decimal form.
    Float(f64),
    /// An arbitrary-precision integer.
    Big(BigInt),
    /// A decimal string: optional sign, digits, optional fraction, optional
    /// exponent (`"-12.5"`, `"1e21"`, `".5"`).
    Text(Cow<'a, str>),
}

macro_rules! from_int {
    ($($t:ty),*) => {$(
        impl From<$t> for Number<'static> {
            fn from(value: $t) -> Self {
                Number::Int(value as i128)
            }
        }
    )*};
}
from_int!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, usize);

impl From<u128> for Number<'static> {
    fn from(value: u128) -> Self {
        Number::Big(BigInt::from(value))
    }
}

impl From<f64> for Number<'static> {
    fn from(value: f64) -> Self {
        Number::Float(value)
    }
}

impl From<f32> for Number<'static> {
    /// Goes through the `f32`'s own shortest decimal form, so `0.1f32` is
    /// "zero point one" rather than the widened `0.100000001490116...`.
    fn from(value: f32) -> Self {
        Number::Text(Cow::Owned(value.to_string()))
    }
}

impl From<BigInt> for Number<'static> {
    fn from(value: BigInt) -> Self {
        Number::Big(value)
    }
}

impl From<&BigInt> for Number<'static> {
    fn from(value: &BigInt) -> Self {
        Number::Big(value.clone())
    }
}

impl From<BigUint> for Number<'static> {
    fn from(value: BigUint) -> Self {
        Number::Big(BigInt::from(value))
    }
}

impl From<&BigUint> for Number<'static> {
    fn from(value: &BigUint) -> Self {
        Number::Big(BigInt::from(value.clone()))
    }
}

impl<'a> From<&'a str> for Number<'a> {
    fn from(value: &'a str) -> Self {
        Number::Text(Cow::Borrowed(value))
    }
}

impl From<String> for Number<'static> {
    fn from(value: String) -> Self {
        Number::Text(Cow::Owned(value))
    }
}

impl<'a> From<&'a String> for Number<'a> {
    fn from(value: &'a String) -> Self {
        Number::Text(Cow::Borrowed(value))
    }
}

impl<'a> From<Cow<'a, str>> for Number<'a> {
    fn from(value: Cow<'a, str>) -> Self {
        Number::Text(value)
    }
}

impl<'a, 'b: 'a> From<&'a Number<'b>> for Number<'a> {
    fn from(value: &'a Number<'b>) -> Self {
        match value {
            Number::Int(i) => Number::Int(*i),
            Number::Float(f) => Number::Float(*f),
            Number::Big(b) => Number::Big(b.clone()),
            Number::Text(t) => Number::Text(Cow::Borrowed(t)),
        }
    }
}

// ============================================================================
// Parsed shapes
// ============================================================================

/// A cardinal input: sign, integer magnitude and the fraction's digits.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Cardinal {
    /// Whether the value is negative (`"-0"` counts, as in the JS library).
    pub negative: bool,
    /// The integer part's magnitude.
    pub integer: BigUint,
    /// The digits after the decimal point, exactly as written (`"007"`), or
    /// `None` when there are none.
    pub fraction: Option<String>,
}

/// A currency amount: sign, major units and minor units.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Money {
    /// Whether the amount is negative.
    pub negative: bool,
    /// Whole major units (dollars, euros, ...).
    pub major: BigUint,
    /// Minor units (cents, ...), at the precision the parser was asked for.
    pub minor: u32,
}

/// Parses a value for the cardinal form: any finite number, negatives and
/// decimals included.
pub(crate) fn parse_cardinal(value: &Number<'_>) -> Result<Cardinal> {
    let decimal = decompose(value)?;
    Ok(Cardinal {
        negative: decimal.negative,
        integer: decimal.integer,
        fraction: decimal.fraction.filter(|f| !f.is_empty()),
    })
}

/// Parses a value for the ordinal form: positive integers only.
pub(crate) fn parse_ordinal(value: &Number<'_>) -> Result<BigUint> {
    const POSITIVE: &str = "Ordinals must be positive integers";
    const WHOLE: &str = "Ordinals must be whole numbers";
    match value {
        Number::Float(f) if !f.is_finite() => {
            return Err(Error::InvalidOrdinal(
                "Ordinals must be finite numbers".into(),
            ));
        }
        Number::Float(f) if f.fract() != 0.0 => return Err(Error::InvalidOrdinal(WHOLE.into())),
        Number::Text(t) => {
            let trimmed = t.trim();
            if trimmed.is_empty() {
                return Err(Error::InvalidOrdinal(
                    "Ordinals cannot be empty strings".into(),
                ));
            }
            if trimmed.starts_with('-') {
                return Err(Error::InvalidOrdinal("Ordinals cannot be negative".into()));
            }
            if trimmed.contains('.') && !trimmed.contains(['e', 'E']) {
                return Err(Error::InvalidOrdinal(WHOLE.into()));
            }
        }
        _ => {}
    }
    let decimal = decompose(value)?;
    if decimal.negative && !decimal.integer.is_zero() {
        return Err(Error::InvalidOrdinal(POSITIVE.into()));
    }
    if decimal
        .fraction
        .is_some_and(|f| f.bytes().any(|b| b != b'0'))
    {
        return Err(Error::InvalidOrdinal(WHOLE.into()));
    }
    if decimal.integer.is_zero() {
        return Err(Error::InvalidOrdinal(POSITIVE.into()));
    }
    Ok(decimal.integer)
}

/// Parses a value for the currency form, keeping `minor_digits` decimal
/// digits (2, or 3 for a thousandth-unit currency) and truncating anything
/// finer: at 2 digits `"1.004"` is one major unit and zero minor units.
///
/// Truncating rather than rejecting is deliberate: float input would
/// otherwise be unusable, since `0.1 + 0.2` is `0.30000000000000004`.
pub(crate) fn parse_currency(value: &Number<'_>, minor_digits: u32) -> Result<Money> {
    let decimal = decompose(value)?;
    let minor = match decimal.fraction {
        None => 0,
        Some(fraction) => {
            let mut digits: String = fraction.chars().take(minor_digits as usize).collect();
            while digits.len() < minor_digits as usize {
                digits.push('0');
            }
            digits.parse().expect("minor digits are ASCII digits")
        }
    };
    Ok(Money {
        negative: decimal.negative,
        major: decimal.integer,
        minor,
    })
}

// ============================================================================
// Decomposition
// ============================================================================

/// The common shape every parser starts from.
struct Decimal {
    negative: bool,
    integer: BigUint,
    /// Fraction digits exactly as written; `Some("")` is possible for `"1."`.
    fraction: Option<String>,
}

/// Largest exponent accepted in scientific notation, to bound the digit
/// string a pathological `"1e999999999"` would otherwise expand to.
const MAX_EXPONENT: i64 = 10_000;

fn decompose(value: &Number<'_>) -> Result<Decimal> {
    match value {
        Number::Int(i) => Ok(Decimal {
            negative: *i < 0,
            integer: BigUint::from(i.unsigned_abs()),
            fraction: None,
        }),
        Number::Big(b) => Ok(Decimal {
            negative: b.sign() == Sign::Minus,
            integer: b.magnitude().clone(),
            fraction: None,
        }),
        Number::Float(f) => {
            if !f.is_finite() {
                return Err(Error::InvalidNumber(
                    "Number must be finite (NaN and Infinity are not supported)".into(),
                ));
            }
            // Fast path for integers that are exact in an f64.
            if f.fract() == 0.0 && f.abs() <= 9_007_199_254_740_992.0 {
                let magnitude = f.abs().to_u64().expect("checked to fit");
                return Ok(Decimal {
                    negative: *f < 0.0,
                    integer: BigUint::from(magnitude),
                    fraction: None,
                });
            }
            // `Display` for f64 is the shortest round-trip form and never uses
            // an exponent, which is exactly the expanded form we want.
            decompose_text(&f.to_string())
        }
        Number::Text(t) => decompose_text(t),
    }
}

fn invalid(text: &str) -> Error {
    Error::InvalidNumber(format!("Invalid number format: {text:?}"))
}

/// Parses a decimal string: optional sign, digits with an optional fraction,
/// optional exponent. Hex, `Infinity` and `NaN` are rejected.
fn decompose_text(text: &str) -> Result<Decimal> {
    let trimmed = text.trim();
    let bytes = trimmed.as_bytes();
    let mut pos = 0;

    let negative = match bytes.first() {
        Some(b'-') => {
            pos += 1;
            true
        }
        Some(b'+') => {
            pos += 1;
            false
        }
        _ => false,
    };

    let int_start = pos;
    while pos < bytes.len() && bytes[pos].is_ascii_digit() {
        pos += 1;
    }
    let int_digits = &trimmed[int_start..pos];

    let mut frac_digits: Option<&str> = None;
    if pos < bytes.len() && bytes[pos] == b'.' {
        pos += 1;
        let frac_start = pos;
        while pos < bytes.len() && bytes[pos].is_ascii_digit() {
            pos += 1;
        }
        frac_digits = Some(&trimmed[frac_start..pos]);
    }

    if int_digits.is_empty() && frac_digits.is_none_or(str::is_empty) {
        return Err(invalid(text));
    }

    let mut exponent: i64 = 0;
    if pos < bytes.len() && (bytes[pos] == b'e' || bytes[pos] == b'E') {
        pos += 1;
        let exp_negative = match bytes.get(pos) {
            Some(b'-') => {
                pos += 1;
                true
            }
            Some(b'+') => {
                pos += 1;
                false
            }
            _ => false,
        };
        let exp_start = pos;
        while pos < bytes.len() && bytes[pos].is_ascii_digit() {
            pos += 1;
        }
        let exp_digits = &trimmed[exp_start..pos];
        if exp_digits.is_empty() {
            return Err(invalid(text));
        }
        let magnitude: i64 = exp_digits
            .trim_start_matches('0')
            .parse()
            .ok()
            .filter(|&m| m <= MAX_EXPONENT)
            .ok_or_else(|| Error::InvalidNumber(format!("Exponent too large in {text:?}")))?;
        exponent = if exp_negative { -magnitude } else { magnitude };
    }

    if pos != bytes.len() {
        return Err(invalid(text));
    }

    let (integer, fraction) = if exponent == 0 {
        (int_digits.to_string(), frac_digits.map(str::to_string))
    } else {
        shift_point(int_digits, frac_digits.unwrap_or(""), exponent)
    };

    let integer = if integer.is_empty() {
        BigUint::zero()
    } else {
        BigUint::parse_bytes(integer.as_bytes(), 10).expect("validated ASCII digits")
    };
    Ok(Decimal {
        negative,
        integer,
        fraction,
    })
}

/// Moves the decimal point of `int.frac` by `exponent` places.
fn shift_point(int_digits: &str, frac_digits: &str, exponent: i64) -> (String, Option<String>) {
    let digits = format!("{int_digits}{frac_digits}");
    let point = int_digits.len() as i64 + exponent;
    if point >= digits.len() as i64 {
        let zeros = "0".repeat((point - digits.len() as i64) as usize);
        (format!("{digits}{zeros}"), None)
    } else if point <= 0 {
        let zeros = "0".repeat((-point) as usize);
        (String::new(), Some(format!("{zeros}{digits}")))
    } else {
        let (i, f) = digits.split_at(point as usize);
        (i.to_string(), Some(f.to_string()))
    }
}

// ============================================================================
// Helpers for language implementations
// ============================================================================

/// Splits `n` into base-`group` digit groups, least significant first
/// (`split_groups(1_234_567, 1000)` is `[567, 234, 1]`). Zero yields `[]`.
pub(crate) fn split_groups(n: &BigUint, group: u32) -> Vec<u32> {
    if let Some(small) = n.to_u64() {
        let mut out = Vec::new();
        let mut rest = small;
        let group = u64::from(group);
        while rest > 0 {
            out.push((rest % group) as u32);
            rest /= group;
        }
        return out;
    }
    let mut out = Vec::new();
    let mut rest = n.clone();
    while !rest.is_zero() {
        out.push((&rest % group).to_u32().expect("remainder below group"));
        rest /= group;
    }
    out
}

/// Splits `n` South Asian style: a first group of `head` (1000) then groups
/// of `rest` (100), least significant first. Zero yields `[]`.
pub(crate) fn split_groups_indian(n: &BigUint, head: u32, rest: u32) -> Vec<u32> {
    if n.is_zero() {
        return Vec::new();
    }
    let mut out = vec![(n % head).to_u32().expect("remainder below head")];
    out.extend(split_groups(&(n / head), rest));
    out
}

/// Parses a string of ASCII digits (a fraction part) into a big integer.
pub(crate) fn digits_to_biguint(digits: &str) -> BigUint {
    BigUint::parse_bytes(digits.as_bytes(), 10).unwrap_or_default()
}

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

    fn big(s: &str) -> BigUint {
        BigUint::parse_bytes(s.as_bytes(), 10).unwrap()
    }

    #[test]
    fn cardinal_from_integers_and_bigints() {
        let c = parse_cardinal(&Number::from(-42)).unwrap();
        assert_eq!((c.negative, c.integer, c.fraction), (true, big("42"), None));
        let c = parse_cardinal(&Number::from(u128::MAX)).unwrap();
        assert_eq!(c.integer, big("340282366920938463463374607431768211455"));
        let c = parse_cardinal(&Number::from(
            BigInt::parse_bytes(b"-123456789012345678901234567890", 10).unwrap(),
        ))
        .unwrap();
        assert!(c.negative);
        assert_eq!(c.integer, big("123456789012345678901234567890"));
    }

    #[test]
    fn cardinal_from_floats_uses_shortest_repr() {
        let c = parse_cardinal(&Number::from(17.42)).unwrap();
        assert_eq!((c.integer, c.fraction.as_deref()), (big("17"), Some("42")));
        let c = parse_cardinal(&Number::from(0.007)).unwrap();
        assert_eq!((c.integer, c.fraction.as_deref()), (big("0"), Some("007")));
        let c = parse_cardinal(&Number::from(1e21)).unwrap();
        assert_eq!(c.integer, big("1000000000000000000000"));
        let c = parse_cardinal(&Number::from(-0.0)).unwrap();
        assert!(!c.negative);
        let c = parse_cardinal(&Number::from(0.1f32)).unwrap();
        assert_eq!(c.fraction.as_deref(), Some("1"));
        assert!(parse_cardinal(&Number::from(f64::NAN)).is_err());
        assert!(parse_cardinal(&Number::from(f64::INFINITY)).is_err());
    }

    #[test]
    fn cardinal_from_strings() {
        let c = parse_cardinal(&Number::from("  -1.500 ")).unwrap();
        assert_eq!(
            (c.negative, c.integer, c.fraction.as_deref()),
            (true, big("1"), Some("500"))
        );
        let c = parse_cardinal(&Number::from(".5")).unwrap();
        assert_eq!((c.integer, c.fraction.as_deref()), (big("0"), Some("5")));
        let c = parse_cardinal(&Number::from("7.")).unwrap();
        assert_eq!((c.integer, c.fraction), (big("7"), None));
        let c = parse_cardinal(&Number::from("+12")).unwrap();
        assert_eq!(c.integer, big("12"));
        let c = parse_cardinal(&Number::from("-0")).unwrap();
        assert!(c.negative);
        for bad in [
            "", "   ", "abc", "1.2.3", "0x1A", "Infinity", "NaN", "1e", "1_000", "--1", "1 2",
        ] {
            assert!(
                parse_cardinal(&Number::from(bad)).is_err(),
                "{bad:?} should be rejected"
            );
        }
    }

    #[test]
    fn scientific_notation_expands_exactly() {
        let c = parse_cardinal(&Number::from("1e21")).unwrap();
        assert_eq!(c.integer, big("1000000000000000000000"));
        let c = parse_cardinal(&Number::from("1.5e3")).unwrap();
        assert_eq!((c.integer, c.fraction), (big("1500"), None));
        let c = parse_cardinal(&Number::from("1.5e-3")).unwrap();
        assert_eq!((c.integer, c.fraction.as_deref()), (big("0"), Some("0015")));
        let c = parse_cardinal(&Number::from("-2.5E+1")).unwrap();
        assert_eq!((c.negative, c.integer, c.fraction), (true, big("25"), None));
        let c = parse_cardinal(&Number::from("12345e-2")).unwrap();
        assert_eq!((c.integer, c.fraction.as_deref()), (big("123"), Some("45")));
        assert!(parse_cardinal(&Number::from("1e99999999")).is_err());
    }

    #[test]
    fn ordinal_rejects_non_positive_and_fractional() {
        assert_eq!(parse_ordinal(&Number::from(42)).unwrap(), big("42"));
        assert_eq!(parse_ordinal(&Number::from(3.0)).unwrap(), big("3"));
        assert_eq!(parse_ordinal(&Number::from("1e3")).unwrap(), big("1000"));
        for bad in [
            Number::from(0),
            Number::from(-1),
            Number::from(1.5),
            Number::from("0"),
            Number::from("-5"),
            Number::from("1.0"),
            Number::from(""),
            Number::from("x"),
        ] {
            assert!(parse_ordinal(&bad).is_err(), "{bad:?} should be rejected");
        }
        assert!(parse_ordinal(&Number::from(f64::INFINITY)).is_err());
    }

    #[test]
    fn currency_truncates_to_minor_digits() {
        let m = parse_currency(&Number::from(42.5), 2).unwrap();
        assert_eq!((m.negative, m.major, m.minor), (false, big("42"), 50));
        let m = parse_currency(&Number::from("1.004"), 2).unwrap();
        assert_eq!((m.major, m.minor), (big("1"), 0));
        let m = parse_currency(&Number::from("1.500"), 3).unwrap();
        assert_eq!((m.major, m.minor), (big("1"), 500));
        let m = parse_currency(&Number::from("0.001"), 3).unwrap();
        assert_eq!((m.major, m.minor), (big("0"), 1));
        let m = parse_currency(&Number::from(-1), 2).unwrap();
        assert_eq!((m.negative, m.major, m.minor), (true, big("1"), 0));
        let m = parse_currency(&Number::from(0.1 + 0.2), 2).unwrap();
        assert_eq!(m.minor, 30);
        let m = parse_currency(&Number::from(BigInt::from(5)), 2).unwrap();
        assert_eq!(m.major, big("5"));
    }

    #[test]
    fn group_splitting() {
        assert_eq!(split_groups(&big("1234567"), 1000), vec![567, 234, 1]);
        assert_eq!(split_groups(&big("0"), 1000), Vec::<u32>::new());
        assert_eq!(
            split_groups(&big("100000000000000000000000"), 1000),
            vec![0, 0, 0, 0, 0, 0, 0, 100]
        );
        assert_eq!(split_groups(&big("123456789"), 10000), vec![6789, 2345, 1]);
        assert_eq!(
            split_groups_indian(&big("12345678"), 1000, 100),
            vec![678, 45, 23, 1]
        );
        assert_eq!(digits_to_biguint("007"), big("7"));
    }
}