num2words2-core 0.1.1

Number-to-words conversion for 120+ languages (Rust port of num2words). 42 -> forty-two.
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
//! Port of `lang_MR.py` (Marathi).
//!
//! Shape: **self-contained**. `Num2Word_MR` subclasses `Num2Word_Base` but
//! defines no `high_numwords`/`mid_numwords`/`low_numwords`, so Python never
//! builds `self.cards` and never sets `MAXVAL`. Every in-scope mode is
//! overridden outright and driven by `_int_to_word`, a plain recursive
//! descent over the Indian scale system (हजार / लाख / कोटी / अब्ज / खर्व).
//! Consequently `cards`/`maxval`/`merge` stay at their trait defaults here.
//!
//! **There is no overflow check and no ceiling.** Because `MAXVAL` is never
//! set, the `value >= self.MAXVAL` guard in `Num2Word_Base.to_cardinal` is
//! never reached (the override skips it), and the top `खर्व` branch recurses
//! on `number // 10**11`, which re-enters the same branch for large enough
//! inputs. So `10**22` is "एक खर्व खर्व" rather than an `OverflowError`, and
//! arbitrarily large `BigInt`s terminate by repeated division. None of the
//! four in-scope modes can raise: every list index is guarded by the range
//! check that selects the branch, so there is no `IndexError`/`KeyError`
//! path here.
//!
//! Inherited from `Num2Word_Base`:
//!   * `setup()` sets only `negword = "ऋण "` (note the **trailing space**)
//!     and `pointword = "दशांश"`. Nothing else is touched, so `is_title`
//!     stays `False` — and MR's `to_cardinal` never calls `self.title`
//!     anyway.
//!
//! # Faithfully reproduced Python oddities
//!
//! This is a port, not a rewrite. The following all look wrong but are
//! exactly what Python emits, and every one is confirmed against the frozen
//! corpus:
//!
//! 1. **Only the 20s compound.** `_int_to_word` glues the unit onto "वीस"
//!    for 21..29 ("एकवीस", "पाचवीस", "चारवीस"), but every other decade is
//!    emitted as two space-separated words in the wrong order for Marathi:
//!    31 is "तीस एक" (lit. "thirty one"), 42 "चाळीस दोन", 99 the hardcoded
//!    "नव्याण्णव". Real Marathi would be एकतीस / बेचाळीस. Preserved verbatim.
//! 2. **The 20s compounds are themselves malformed.** 22 → `ones[2] + "वीस"`
//!    = "दोनवीस" (Marathi: बावीस), 23 → "तीनवीस" (Marathi: तेवीस). Only 21
//!    ("एकवीस") happens to come out right.
//! 3. **The `number == 21` special case is dead code.** It produces
//!    `"एक" + tens[2]`, but `ones[21 % 10]` is *already* "एक", so both arms
//!    of the conditional agree. Reproduced anyway — see [`int_to_word`].
//! 4. **`99` is special-cased but `98`/`97`/... are not**, so 99 is
//!    "नव्याण्णव" while 98 is "नव्वद आठ".
//! 5. **`to_ordinal` suffixes the raw cardinal**, so negatives and zero
//!    produce nonsense rather than raising: `to_ordinal(0)` == "शून्यवा",
//!    `to_ordinal(-21)` == "ऋण एकवीसवा", `to_ordinal(-1000)` ==
//!    "ऋण एक हजारवा". `Num2Word_Base.verify_ordinal` (which would raise
//!    `TypeError` on a negative) is never called.
//! 6. **`to_ordinal_num` mixes numeral systems.** 1..4 return Devanagari
//!    digits ("१ला", "२रा", "३रा", "४था"); everything else falls through to
//!    `str(number) + "वा"`, which is ASCII — hence "5वा", "10वा", "0वा" and
//!    even "-1वा". Also 2 and 3 share the suffix "रा".
//! 7. **`to_year` ignores its `longval` parameter** and just prefixes the
//!    cardinal, so there is no two-digit-pair year reading: 1905 is
//!    "सन एक हजार नऊशे पाच", not "nineteen oh five". `to_year(0)` is
//!    "सन शून्य" (0 is not `< 0`).
//!
//! 8. **`to_currency` falls back to rupees for every unknown code** rather
//!    than raising, because it looks the code up with
//!    `CURRENCY_FORMS.get(currency, CURRENCY_FORMS["INR"])`. The inherited
//!    `to_cheque` uses a strict `CURRENCY_FORMS[currency]` subscript instead,
//!    so the *same* code (JPY, CHF, ...) renders happily as रुपये through
//!    `to_currency` but raises NotImplementedError through `to_cheque`. Both
//!    halves of that contradiction are in the corpus.
//!
//! # Currency shape
//!
//! `to_currency` is overridden outright and shares nothing with
//! `Num2Word_Base`'s currency machinery — see [`LangMr::to_currency`].
//! `to_cheque` is *not* overridden, so it comes from `Num2Word_Base` and needs
//! only `lang_name` + `currency_forms` + the default `money_verbose` (which
//! routes to MR's `to_cardinal`) to reproduce
//! "एक हजार दोनशे तीस चार AND 56/100 युरो". `.upper()` is a no-op on
//! Devanagari — it is caseless — so only the literal "AND" looks upper-cased.
//! `pluralize` is never reached from either path and correctly keeps the
//! trait's raising default (`Num2Word_Base.pluralize` raises
//! NotImplementedError too).
//!
//! Note also that the `number < 0` arm of `_int_to_word` (which would return
//! `negword + _int_to_word(abs(n))` with **no** `.strip()`) is unreachable
//! from every in-scope entry point: `to_cardinal` detaches the sign before
//! calling in, and `to_ordinal`/`to_year` both route through `to_cardinal`.
//! It is reproduced in [`int_to_word`] for fidelity regardless.

use crate::base::{Lang, N2WError, Result};
use crate::currency::{CurrencyForms, CurrencyValue};
use crate::floatpath::FloatValue;
use crate::strnum::{python_decimal_parse, ParsedNumber};
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::{Signed, ToPrimitive, Zero};
use std::collections::HashMap;

/// `self.negword`, set by `setup()`. The trailing space is load-bearing:
/// `to_cardinal` concatenates it directly (`ret + word`) rather than joining
/// with a separator, unlike `Num2Word_Base`, which does `negword.strip() + " "`.
const NEGWORD: &str = "ऋण ";

/// `self.pointword`, set by `setup()`. Reached only on the float/Decimal
/// path, where `Num2Word_MR.to_cardinal` splices it between the integer and
/// the digit-by-digit fraction (see [`cardinal_from_str`]). `is_title` is
/// `False`, and MR uses `self.pointword` directly rather than `self.title(...)`,
/// so it is emitted verbatim.
const POINTWORD: &str = "दशांश";

const ZERO_WORD: &str = "शून्य";

/// `ones`. Index 0 is "" and is only ever selected when the caller has
/// already established `number % 10 != 0` (or `number != 0`), so the empty
/// string never reaches the output.
const ONES: [&str; 10] = [
    "", "एक", "दोन", "तीन", "चार", "पाच", "सहा", "सात", "आठ", "नऊ",
];

/// `tens`. Index 0 is unreachable (`number >= 20` in the only branch that
/// reads this table) and index 1 is dead too — 10..19 are handled by
/// [`TEENS`] before the `< 100` branch is ever entered.
const TENS: [&str; 10] = [
    "", "दहा", "वीस", "तीस", "चाळीस", "पन्नास", "साठ", "सत्तर", "ऐंशी", "नव्वद",
];

/// `teens`, indexed by `number - 10` for 10..=19.
const TEENS: [&str; 10] = [
    "दहा", "अकरा", "बारा", "तेरा", "चौदा", "पंधरा", "सोळा", "सतरा", "अठरा", "एकोणीस",
];

/// Hardcoded in the `number // 10 == 9 and number % 10 == 9` arm.
const NINETY_NINE: &str = "नव्याण्णव";

/// Suffix appended by the `< 1000` branch: `ones[number // 100] + "शे"`.
const HUNDRED_SUFFIX: &str = "शे";

/// Generic ordinal suffix appended to the cardinal for anything above 10.
const ORDINAL_SUFFIX: &str = "वा";

/// `to_ordinal`'s irregular forms for 1..=10. Python tests `number == k`
/// with plain `==`, which is *numeric*, so these fire for equal
/// floats/Decimals too: `to_ordinal(1.0)` is "पहिला" and
/// `to_ordinal(Decimal("5.00"))` is "पाचवा" — the ".0" tail vanishes
/// entirely. Shared by [`LangMr::to_ordinal`] (int path) and
/// [`LangMr::ordinal_float_entry`] (float/Decimal path).
const ORDINAL_SPECIALS: [(u64, &str); 10] = [
    (1, "पहिला"),
    (2, "दुसरा"),
    (3, "तिसरा"),
    (4, "चौथा"),
    (5, "पाचवा"),
    (6, "सहावा"),
    (7, "सातवा"),
    (8, "आठवा"),
    (9, "नववा"),
    (10, "दहावा"),
];

const YEAR_PREFIX: &str = "सन ";
const YEAR_BC_PREFIX: &str = "इसवीसन पूर्व ";

/// `Num2Word_MR.to_currency`'s own default `separator=" आणि "`.
///
/// See [`SEPARATOR_UNSET`] for why this cannot simply be a parameter default.
const SEPARATOR_DEFAULT: &str = " आणि ";

/// The separator the pyo3 binding passes when the Python caller omitted one.
///
/// `Num2Word_MR.to_currency` declares `separator=" आणि "`, but the `Lang` trait
/// has no per-language defaults: `__init__.py`'s fast path (and
/// `bench/diff_test.py`) substitute `kwargs.get("separator", ",")` —
/// **`Num2Word_Base`'s** default — before the value ever reaches Rust. By then
/// "caller omitted separator" and "caller explicitly passed a comma" are the
/// same string, and the information needed to tell them apart no longer exists
/// on this side of the boundary.
///
/// So `,` is read back as the unset sentinel and MR's own default restored.
/// This is the only reading that matches the oracle: every float row of the
/// `mr` currency corpus was generated by `num2words(v, lang="mr",
/// to="currency", currency=c)` with no `separator=`, and each one expects
/// " आणि " (e.g. `12.34` -> "बारा युरो आणि तीस चार सेंट्स").
///
/// The cost is narrow and known: a caller who *explicitly* passes
/// `separator=","` gets " आणि " here where Python would give ",". Fixing that
/// properly needs `Option<&str>` in the trait signature, which lives in
/// `base.rs` — outside this port's remit. Flagged in the port report.
/// `lang_ca.rs` and `lang_es.rs` resolve the identical conflict the same way.
const SEPARATOR_UNSET: &str = ",";

/// The fallback code for `CURRENCY_FORMS.get(currency, CURRENCY_FORMS["INR"])`.
const CURRENCY_FALLBACK: &str = "INR";

fn bi(n: u64) -> BigInt {
    BigInt::from(n)
}

/// Narrow a `BigInt` the caller has already range-checked below 1000.
///
/// Every call site sits inside a `number < 100` or `number < 1000` guard and
/// passes either the value itself or a quotient of it, so the conversion
/// cannot fail.
fn small(n: &BigInt) -> usize {
    n.to_usize()
        .expect("caller guarantees this value is range-checked below 1000")
}

/// One Indian-scale group: `_int_to_word(number // divisor) + " " + word`,
/// then `" " + _int_to_word(remainder)` when the remainder is non-zero.
///
/// Shared by the हजार / लाख / कोटी / अब्ज / खर्व branches, which are
/// character-for-character identical in Python apart from the two constants.
fn scale(number: &BigInt, divisor: u64, word: &str) -> String {
    let (div, rem) = number.div_mod_floor(&bi(divisor));
    let mut result = format!("{} {}", int_to_word(&div), word);
    if !rem.is_zero() {
        result.push(' ');
        result.push_str(&int_to_word(&rem));
    }
    result
}

/// Port of `Num2Word_MR._int_to_word`.
///
/// Python checks `number == 0` first, then the tables are (re)declared, then
/// the `number < 0` arm runs — so every branch below the sign check operates
/// on a non-negative value and Python's floor `//`/`%` coincide with
/// truncating division. `div_mod_floor` matches Python exactly either way.
fn int_to_word(number: &BigInt) -> String {
    if number.is_zero() {
        return ZERO_WORD.to_string();
    }

    // Unreachable from to_cardinal/to_ordinal/to_year (all detach the sign
    // first), but reproduced as written — note there is no .strip() here.
    if number.is_negative() {
        return format!("{}{}", NEGWORD, int_to_word(&number.abs()));
    }

    if number < &bi(10) {
        return ONES[small(number)].to_string();
    }

    if number < &bi(20) {
        return TEENS[small(number) - 10].to_string();
    }

    if number < &bi(100) {
        let n = small(number);
        let tens_digit = n / 10;
        let ones_digit = n % 10;
        let mut result = TENS[tens_digit].to_string();
        if ones_digit != 0 {
            if tens_digit == 2 {
                // Python: `"एक" + tens[2] if number == 21 else ones[number % 10] + tens[2]`
                // — the conditional binds looser than `+`, so both arms
                // concatenate onto tens[2]. The n == 21 arm is dead code:
                // ONES[1] is already "एक". Kept for fidelity.
                result = if n == 21 {
                    format!("एक{}", TENS[2])
                } else {
                    format!("{}{}", ONES[ones_digit], TENS[2])
                };
            } else if tens_digit == 9 && ones_digit == 9 {
                result = NINETY_NINE.to_string();
            } else {
                // Python: `result += " " + ones[number % 10] if number % 10 else ""`.
                // The trailing conditional is redundant — we are already
                // inside `if number % 10:` — so the else-branch never fires.
                result.push(' ');
                result.push_str(ONES[ones_digit]);
            }
        }
        return result;
    }

    if number < &bi(1000) {
        let (div, rem) = number.div_mod_floor(&bi(100));
        let mut result = format!("{}{}", ONES[small(&div)], HUNDRED_SUFFIX);
        if !rem.is_zero() {
            result.push(' ');
            result.push_str(&int_to_word(&rem));
        }
        return result;
    }

    if number < &bi(100_000) {
        // Indian lakh system: thousands run 1_000..99_999.
        return scale(number, 1_000, "हजार");
    }

    if number < &bi(10_000_000) {
        return scale(number, 100_000, "लाख");
    }

    if number < &bi(1_000_000_000) {
        // "Less than 100 crore" — the quotient here reaches 99.
        return scale(number, 10_000_000, "कोटी");
    }

    if number < &bi(100_000_000_000) {
        return scale(number, 1_000_000_000, "अब्ज");
    }

    // Kharab, and the only unbounded branch: 10**22 // 10**11 == 10**11
    // lands right back here, yielding "एक खर्व खर्व".
    scale(number, 100_000_000_000, "खर्व")
}

// ---- float / Decimal path -------------------------------------------------
//
// `Num2Word_MR.to_cardinal` does NOT use `Num2Word_Base.to_cardinal_float`.
// It overrides `to_cardinal` and handles non-integers *inline* off
// `str(number)`:
//
// ```python
// n = str(number).strip()
// if n.startswith("-"):
//     n = n[1:]; ret = self.negword
// else:
//     ret = ""
// if "." in n:
//     left, right = n.split(".", 1)
//     ret += self._int_to_word(int(left)) + " " + self.pointword + " "
//     ret += " ".join(self._int_to_word(int(d)) for d in right)
//     return ret.strip()
// else:
//     return (ret + self._int_to_word(int(n))).strip()
// ```
//
// So the fractional part is the *literal digit string* of `str(number)`
// spoken one digit at a time — never a `post` integer, never zero-padded to a
// precision, and the `base.float2tuple` `< 0.01` heuristic never runs. The
// entire specification is therefore Python's `repr(float)` / `str(Decimal)`,
// reproduced by [`py_float_repr`] / [`py_decimal_str`]. Two behaviours fall
// straight out of that and are exactly what Python does, but which the
// inherited base path gets wrong (which is why MR needs its own override):
//
//   * **negative zero keeps its sign.** `str(-0.0)` == "-0.0" and
//     `str(Decimal("-0.00"))` == "-0.00" both start with "-", so MR prepends
//     `negword` even though the value is not `< 0`. Base drops it (its guard
//     is `value < 0 and pre == 0`).
//   * **scientific notation raises `ValueError`.** For `abs(f) >= 1e16` or a
//     nonzero `abs(f) < 1e-4`, `repr(float)` switches to exponent form; MR
//     then feeds a token containing `e`/`E`/`+` to `int()` (either `int(n)`
//     with no dot, or `int(d)` mid-fraction) and Python raises. Base renders
//     it instead.
//
// `precision` (and the `precision=` kwarg) is set on the converter by
// `__init__.py` but MR's `to_cardinal` never reads `self.precision`, so it is
// ignored here too — `precision_override` is dropped in `to_cardinal_float`.
//
// These helpers mirror the already-verified `lang_as.rs` / `lang_gu.rs`
// (Assamese / Gujarati) versions verbatim; those languages share MR's exact
// `str(number)`-driven shape.

/// Shortest round-trip decimal digits of `a` and its decimal point position,
/// matching CPython/Gay's dtoa. Returns `(digits, decpt)` where the value is
/// `0.<digits> * 10**decpt`.
///
/// Rust's `{:e}` is also shortest-round-trip and agrees with dtoa on the digit
/// *count* and almost always the digits; it disagrees only on **exact ties**,
/// where dtoa picks the candidate with an even last digit while Rust rounds
/// half up. The block below detects that tie with no bignum (write
/// `a = m * 2**e` with `m` odd, `q = digits.len() - decpt`; the tie is
/// `e + q + 1 == 0`, plus `5**-q | m` when `q < 0`) and steps to the even
/// neighbour, exactly as `lang_as.rs` documents and fuzzes.
fn shortest_digits(a: f64) -> (String, i32) {
    let sci = format!("{:e}", a);
    let (mant, exp) = sci
        .split_once('e')
        .expect("{:e} on a finite f64 always emits an exponent");
    let mut digits: Vec<u8> = mant.bytes().filter(|c| *c != b'.').collect();
    let mut decpt: i32 = exp.parse::<i32>().expect("{:e} exponent is an integer") + 1;

    // Decompose a == m * 2**e exactly, then reduce m to odd.
    let bits = a.to_bits();
    let biased = ((bits >> 52) & 0x7ff) as i32;
    let frac = bits & ((1u64 << 52) - 1);
    let (mut m, mut e) = if biased == 0 {
        (frac, -1074i32) // subnormal: no implicit leading bit
    } else {
        (frac | (1u64 << 52), biased - 1075)
    };
    if m == 0 {
        // a == 0.0: dtoa reports digits "0", decpt 1. No tie to break.
        return (String::from_utf8(digits).expect("ASCII digits"), decpt);
    }
    let z = m.trailing_zeros() as i32;
    m >>= z;
    e += z;

    let q = digits.len() as i32 - decpt;
    let mut tie = e + q + 1 == 0;
    if tie && q < 0 {
        let r = -q as u32;
        tie = r <= 22 && m % 5u64.pow(r) == 0;
    }
    if !tie {
        return (String::from_utf8(digits).expect("ASCII digits"), decpt);
    }

    let last = digits[digits.len() - 1] - b'0';
    if last % 2 == 1 {
        if m % 4 == 1 {
            // k even: Python wants k, Rust gave k+1. Last digit is odd (hence
            // non-zero), so this never borrows.
            *digits.last_mut().expect("non-empty") -= 1;
        } else {
            // k odd: Python wants k+1, Rust gave k. Carry like dtoa's roundoff.
            let mut i = digits.len();
            loop {
                if i == 0 {
                    digits.insert(0, b'1');
                    decpt += 1;
                    break;
                }
                i -= 1;
                if digits[i] == b'9' {
                    digits[i] = b'0';
                } else {
                    digits[i] += 1;
                    break;
                }
            }
        }
        while digits.len() > 1 && *digits.last().expect("non-empty") == b'0' {
            digits.pop();
        }
    }
    (String::from_utf8(digits).expect("ASCII digits"), decpt)
}

/// Python's `str(float)` (== `repr(float)`), CPython's
/// `format_float_short(..., 'r', ...)`. Rust's own `{}` cannot stand in: it
/// never switches to exponent notation and prints `1`, not `1.0`, for integral
/// floats. Rules from `format_float_short`: exponent notation iff
/// `decpt <= -4 || decpt > 16`; the exponent is `%+.02d`; a trailing `.0` is
/// appended to an otherwise-integral fixed result but never in exponent form;
/// `nan` drops its sign, `inf` keeps it.
fn py_float_repr(value: f64) -> String {
    if value.is_nan() {
        return "nan".to_string();
    }
    if value.is_infinite() {
        return if value > 0.0 { "inf" } else { "-inf" }.to_string();
    }
    // is_sign_negative, not `< 0.0`: str(-0.0) is "-0.0", and the sign is
    // stripped textually into a negword.
    let sign = if value.is_sign_negative() { "-" } else { "" };
    let (digits, decpt) = shortest_digits(value.abs());
    let ndigits = digits.len() as i32;

    if decpt <= -4 || decpt > 16 {
        let exp = decpt - 1;
        let mut mant = String::from(&digits[..1]);
        if digits.len() > 1 {
            mant.push('.');
            mant.push_str(&digits[1..]);
        }
        format!(
            "{}{}e{}{:02}",
            sign,
            mant,
            if exp < 0 { '-' } else { '+' },
            exp.abs()
        )
    } else if decpt <= 0 {
        format!("{}0.{}{}", sign, "0".repeat(-decpt as usize), digits)
    } else if decpt >= ndigits {
        format!(
            "{}{}{}.0",
            sign,
            digits,
            "0".repeat((decpt - ndigits) as usize)
        )
    } else {
        let d = decpt as usize;
        format!("{}{}.{}", sign, &digits[..d], &digits[d..])
    }
}

/// Python's `str(Decimal)` — `Decimal.__str__` with the default context.
///
/// A `BigDecimal`'s `(int_val, scale)` is exactly `Decimal`'s `(_int, _exp)`
/// with `_exp == -scale`; the shim builds the value with
/// `BigDecimal::from_str(str(value))`, which preserves trailing zeros and
/// negative exponents (so `"1.10"` round-trips as `(110, 2)`). The
/// `leftdigits > -6` guard keeps every `"0".repeat(...)` bounded.
///
/// Caveat (same as `lang_as.rs`): a `BigDecimal` cannot represent `-0.0`, so a
/// fixed-notation negative zero loses its negword here. Beyond the exponent
/// threshold `int()` raises `ValueError` with a message that agrees either way.
/// Flagged in the port report.
fn py_decimal_str(value: &BigDecimal) -> String {
    let (int_val, scale) = value.as_bigint_and_exponent();
    // i128 so that `-scale` cannot overflow for a pathological i64::MIN scale.
    let exp = -(scale as i128);
    let sign = if int_val.is_negative() { "-" } else { "" };
    let int_digits = int_val.abs().to_string(); // Decimal._int
    let len = int_digits.len() as i128;

    let leftdigits = exp + len;
    let dotplace = if exp <= 0 && leftdigits > -6 {
        leftdigits
    } else {
        1
    };

    let (intpart, fracpart) = if dotplace <= 0 {
        (
            "0".to_string(),
            format!(".{}{}", "0".repeat(-dotplace as usize), int_digits),
        )
    } else if dotplace >= len {
        (
            format!("{}{}", int_digits, "0".repeat((dotplace - len) as usize)),
            String::new(),
        )
    } else {
        let d = dotplace as usize;
        (int_digits[..d].to_string(), format!(".{}", &int_digits[d..]))
    };

    let expstr = if leftdigits == dotplace {
        String::new()
    } else {
        // "%+d" — signed, but not zero-padded, unlike repr(float)'s "%+.02d".
        let d = leftdigits - dotplace;
        format!("E{}{}", if d < 0 { '-' } else { '+' }, d.abs())
    };

    format!("{}{}{}{}", sign, intpart, fracpart, expstr)
}

/// Python's `int(s)` for the ASCII fragments [`cardinal_from_str`] hands it.
/// Ports the underscore rule (`int("1_0") == 10`, `int("1_")` raises) and the
/// error message (`repr(s)` of the original argument).
fn py_int(s: &str) -> Result<BigInt> {
    let err = || {
        N2WError::Value(format!(
            "invalid literal for int() with base 10: '{}'",
            s
        ))
    };
    let t = s.trim();
    let (negative, body) = match t.strip_prefix('-') {
        Some(rest) => (true, rest),
        None => (false, t.strip_prefix('+').unwrap_or(t)),
    };
    if body.is_empty()
        || body.starts_with('_')
        || body.ends_with('_')
        || body.contains("__")
        || !body.chars().all(|c| c.is_ascii_digit() || c == '_')
    {
        return Err(err());
    }
    let digits: String = body.chars().filter(|c| *c != '_').collect();
    let n: BigInt = digits.parse().map_err(|_| err())?;
    Ok(if negative { -n } else { n })
}

/// The body of `Num2Word_MR.to_cardinal` for a stringified number `n`, minus
/// the outer `.strip()` (applied by the caller). The sign is stripped
/// *textually* (so "-0.0" keeps its `negword`), `split(".", 1)` caps at one
/// split (a second dot detonates in the digit loop), and `int(left)` runs
/// before the digit generator so a malformed left half is the first to raise.
fn cardinal_from_str(number: &str) -> Result<String> {
    let n = number.trim();
    let (n, mut ret) = match n.strip_prefix('-') {
        // ret = self.negword — "ऋण " already carries its own trailing space.
        Some(rest) => (rest, NEGWORD.to_string()),
        None => (n, String::new()),
    };

    let Some(dot) = n.find('.') else {
        ret.push_str(&int_to_word(&py_int(n)?));
        return Ok(ret);
    };

    // n.split(".", 1) — maxsplit=1, so `right` keeps any further dots.
    let (left, right) = (&n[..dot], &n[dot + 1..]);
    ret.push_str(&int_to_word(&py_int(left)?));
    ret.push(' ');
    ret.push_str(POINTWORD);
    ret.push(' ');

    // " ".join(self._int_to_word(int(d)) for d in right) — iterate *characters*.
    let mut first = true;
    for d in right.chars() {
        if !first {
            ret.push(' ');
        }
        first = false;
        let mut buf = [0u8; 4];
        ret.push_str(&int_to_word(&py_int(d.encode_utf8(&mut buf))?));
    }
    Ok(ret)
}

/// Marathi (`mr`).
///
/// Stateless apart from the immutable `CURRENCY_FORMS` table — Python's
/// `Num2Word_MR` carries no cross-call flags (`setup` assigns two string
/// constants and nothing mutates them afterwards, and `CURRENCY_FORMS` is a
/// class attribute that is only ever read).
pub struct LangMr {
    /// `Num2Word_MR.CURRENCY_FORMS`, built once. `Num2Word_Base` declares an
    /// empty dict and MR replaces it outright — there is no `__init__` merge
    /// step in this class, and MR inherits neither `CURRENCY_ADJECTIVES` nor
    /// `CURRENCY_PRECISION` overrides (both stay `{}`), so the trait defaults
    /// for `currency_adjective` (None) and `currency_precision` (100) are
    /// already correct and are deliberately not overridden.
    currency_forms: HashMap<&'static str, CurrencyForms>,
}

impl LangMr {
    pub fn new() -> Self {
        // Exactly the four codes Num2Word_MR declares, each a 2-tuple of
        // 2-tuples. Note USD/EUR/GBP repeat the singular as the plural
        // ("डॉलर"/"डॉलर", "पेन्स"/"पेन्स"); only INR actually inflects
        // (रुपया -> रुपये, पैसा -> पैसे). The arity is load-bearing:
        // `to_currency` indexes `cr1[1]`/`cr2[1]` for the plural.
        let mut currency_forms = HashMap::new();
        currency_forms.insert(
            "INR",
            CurrencyForms::new(&["रुपया", "रुपये"], &["पैसा", "पैसे"]),
        );
        currency_forms.insert(
            "USD",
            CurrencyForms::new(&["डॉलर", "डॉलर"], &["सेंट", "सेंट्स"]),
        );
        currency_forms.insert(
            "EUR",
            CurrencyForms::new(&["युरो", "युरो"], &["सेंट", "सेंट्स"]),
        );
        currency_forms.insert(
            "GBP",
            CurrencyForms::new(&["पाउंड", "पाउंड"], &["पेन्स", "पेन्स"]),
        );
        LangMr { currency_forms }
    }
}

impl Default for LangMr {
    fn default() -> Self {
        Self::new()
    }
}

impl Lang for LangMr {

    fn cardinal_float_entry(
        &self,
        value: &crate::floatpath::FloatValue,
        precision_override: Option<u32>,
    ) -> crate::base::Result<String> {
        // Python's to_cardinal routes every float/Decimal through this
        // language's own decimal grammar — 5.0 keeps its ".0" tail
        // ("comma nulla"), unlike Base's whole-value integer route.
        self.to_cardinal_float(value, precision_override)
    }

    /// `to_ordinal(float/Decimal)`. Python's `to_ordinal` opens with ten
    /// `number == k` checks, and `==` is numeric, so whole floats/Decimals
    /// take the irregular words: `to_ordinal(1.0)` == "पहिला",
    /// `to_ordinal(Decimal("5.00"))` == "पाचवा" — the fractional tail
    /// vanishes entirely. Everything else falls through to
    /// `to_cardinal(number) + "वा"`, i.e. the `str(number)` grammar with the
    /// suffix glued on: 11.0 → "अकरा दशांश शून्यवा", 0.5 →
    /// "शून्य दशांश पाचवा". -0.0 equals no special (and 0 has none anyway)
    /// and keeps its textual negword: "ऋण शून्य दशांश शून्यवा"; -3.0 is
    /// likewise "ऋण तीन दशांश शून्यवा" (oddity 5: no negative guard).
    /// Exponent-form inputs (1e16, `Decimal("1E+2")`) miss every `==` and
    /// then raise `int()`'s ValueError from inside `to_cardinal`.
    fn ordinal_float_entry(&self, value: &FloatValue) -> Result<String> {
        if let Some(i) = value.as_whole_int() {
            for (k, word) in ORDINAL_SPECIALS {
                if i == bi(k) {
                    return Ok(word.to_string());
                }
            }
        }
        Ok(format!(
            "{}{}",
            self.to_cardinal_float(value, None)?,
            ORDINAL_SUFFIX
        ))
    }

    /// `to_ordinal_num(float/Decimal)`. Same numeric `==` on 1..=4, so 1.0 →
    /// "१ला", 2.0 → "२रा", 3.0 → "३रा", `Decimal("4.00")` → "४था"; everything
    /// else is `str(number) + "वा"` (oddity 6's numeral-system mix, now with
    /// a decimal point in it). `repr_str` is the binding's Python
    /// `str(value)`, exactly the string Python concatenates — `str()` never
    /// raises, so 5.0 keeps its tail ("5.0वा") and exponent forms pass
    /// through unharmed: "1e+16वा", "1E+2वा", "-0.0वा".
    fn ordinal_num_float_entry(&self, value: &FloatValue, repr_str: &str) -> Result<String> {
        if let Some(i) = value.as_whole_int() {
            if i == bi(1) {
                return Ok("१ला".to_string());
            }
            if i == bi(2) {
                return Ok("२रा".to_string());
            }
            if i == bi(3) {
                return Ok("३रा".to_string());
            }
            if i == bi(4) {
                return Ok("४था".to_string());
            }
        }
        Ok(format!("{}{}", repr_str, ORDINAL_SUFFIX))
    }

    /// `to_year(float/Decimal)`. Python: `if val < 0:` — a *numeric* test,
    /// not a sign-bit one, so -0.0 takes the "सन" arm and keeps the negword
    /// its `to_cardinal` derives textually: "सन ऋण शून्य दशांश शून्य"
    /// (`FloatValue::is_negative()` would lie here — it is sign-bit aware by
    /// design). True negatives are `abs()`ed *before* `to_cardinal`, so the
    /// BC arm never shows a negword: `to_year(-1.5)` ==
    /// "इसवीसन पूर्व एक दशांश पाच". Exponent-form ValueErrors propagate from
    /// `to_cardinal` in both arms.
    fn year_float_entry(&self, value: &FloatValue) -> Result<String> {
        let below_zero = match value {
            // `-0.0 < 0` is False in Python.
            FloatValue::Float { value, .. } => *value < 0.0,
            // A BigInt mantissa carries no negative zero, so is_negative()
            // is already the numeric test on this arm.
            FloatValue::Decimal { value, .. } => value.is_negative(),
        };
        if below_zero {
            let abs = match value {
                FloatValue::Float { value, precision } => FloatValue::Float {
                    value: value.abs(),
                    precision: *precision,
                },
                FloatValue::Decimal { value, precision } => FloatValue::Decimal {
                    value: value.abs(),
                    precision: *precision,
                },
            };
            Ok(format!(
                "{}{}",
                YEAR_BC_PREFIX,
                self.to_cardinal_float(&abs, None)?
            ))
        } else {
            Ok(format!(
                "{}{}",
                YEAR_PREFIX,
                self.to_cardinal_float(value, None)?
            ))
        }
    }

    /// MR inherits Base's `str_to_number` (`Decimal(value)`), which parses
    /// "Infinity"/"-Infinity" *successfully* — the ValueError Python shows
    /// for `num2words("Infinity", lang="mr")` happens later, inside MR's
    /// `to_cardinal` (`int("Infinity")` → `ValueError: invalid literal for
    /// int() with base 10: 'Infinity'`; "-Infinity" strips its sign
    /// textually first and reports 'Infinity' too). The Rust dispatcher
    /// hard-codes Base's `int(Decimal("Infinity"))` semantics
    /// (`OverflowError`) for `ParsedNumber::Inf`, which MR never executes,
    /// so Inf parses punt to the Python fallback instead: it runs the
    /// original converter and reproduces every mode byte for byte
    /// (cardinal/ordinal/year raise the ValueError; `to_ordinal_num` happily
    /// returns "Infinityवा"). NaN stays on the dispatcher path — its
    /// hard-coded ValueError already matches the type MR raises from
    /// `int("NaN")`. Same shape as `lang_be.rs` / `lang_as.rs`.
    fn str_to_number(&self, s: &str) -> Result<ParsedNumber> {
        let parsed = python_decimal_parse(s)?;
        Ok(parsed)
    }

    /// `Decimal('Infinity')` / `-Infinity` per mode. MR's `to_cardinal`
    /// feeds the sign-stripped `str(number)` to `int()`, so `int("Infinity")`
    /// raises `ValueError` for cardinal/ordinal/year ("-Infinity" strips its
    /// sign textually first, so the message names 'Infinity' either way).
    /// `to_ordinal_num` never parses — it is `str(number) + "वा"` — so it
    /// succeeds: "Infinityवा" / "-Infinityवा". Replaces the base default
    /// (`int(Decimal Inf)` → OverflowError), which MR never executes.
    fn inf_result(&self, negative: bool, to: &str) -> Result<String> {
        match to {
            "ordinal_num" => Ok(format!(
                "{}Infinity{}",
                if negative { "-" } else { "" },
                ORDINAL_SUFFIX
            )),
            _ => Err(N2WError::Value(
                "invalid literal for int() with base 10: 'Infinity'".to_string(),
            )),
        }
    }

    /// `Decimal('NaN')` per mode. cardinal/ordinal die in `int("NaN")` →
    /// `ValueError`; `to_ordinal_num` returns "NaNवा" unparsed; but `to_year`
    /// opens with `if val < 0:` — and `Decimal('NaN') < 0` raises
    /// `decimal.InvalidOperation` *before* `to_cardinal` is ever reached.
    fn nan_result(&self, to: &str) -> Result<String> {
        match to {
            "ordinal_num" => Ok(format!("NaN{}", ORDINAL_SUFFIX)),
            "year" => Err(N2WError::Custom {
                module: "decimal",
                class: "InvalidOperation",
                msg: "[<class 'decimal.InvalidOperation'>]".to_string(),
            }),
            _ => Err(N2WError::Value(
                "invalid literal for int() with base 10: 'NaN'".to_string(),
            )),
        }
    }

    /// This language's own `to_currency(currency=...)` default,
    /// read from the live Python signature. Only 44 of 156 use EUR.
    fn default_currency(&self) -> &str {
        "INR"
    }

    /// This language's own `to_currency(separator=...)` default,
    /// read from the live Python signature. Base's is ",", but only
    /// 36 of 149 languages actually use it — most default to " " or a
    /// conjunction, so inheriting Base's comma silently corrupts them.
    fn default_separator(&self) -> &str {
        " आणि "
    }

    fn negword(&self) -> &str {
        NEGWORD
    }

    fn pointword(&self) -> &str {
        "दशांश"
    }

    /// Port of `Num2Word_MR.to_cardinal`, integer path only.
    ///
    /// Python does `n = str(number).strip()` and tests `n.startswith("-")`,
    /// which for an integer is exactly a sign test. The `"." in n` branch
    /// (float input) is out of scope: `str(int)` never contains a dot.
    ///
    /// The final `.strip()` is reproduced but cannot bite — `_int_to_word`
    /// never returns an empty or space-padded string, so the only whitespace
    /// in play is the single space inside `negword`.
    fn to_cardinal(&self, value: &BigInt) -> Result<String> {
        let (ret, n) = if value.is_negative() {
            (NEGWORD, value.abs())
        } else {
            ("", value.clone())
        };
        Ok(format!("{}{}", ret, int_to_word(&n)).trim().to_string())
    }

    /// Port of `Num2Word_MR.to_ordinal`.
    ///
    /// 1..=10 are irregular; everything else — including 0 and every
    /// negative — is the cardinal with "वा" glued on. Never raises.
    fn to_ordinal(&self, value: &BigInt) -> Result<String> {
        for (k, word) in ORDINAL_SPECIALS {
            if value == &bi(k) {
                return Ok(word.to_string());
            }
        }
        Ok(format!("{}{}", self.to_cardinal(value)?, ORDINAL_SUFFIX))
    }

    /// Port of `Num2Word_MR.to_ordinal_num`.
    ///
    /// Devanagari digits for 1..=4, ASCII `str(number)` for everything else
    /// (including negatives: `to_ordinal_num(-1)` == "-1वा"). See oddity 6.
    fn to_ordinal_num(&self, value: &BigInt) -> Result<String> {
        if value == &bi(1) {
            return Ok("१ला".to_string());
        }
        if value == &bi(2) {
            return Ok("२रा".to_string());
        }
        if value == &bi(3) {
            return Ok("३रा".to_string());
        }
        if value == &bi(4) {
            return Ok("४था".to_string());
        }
        Ok(format!("{}{}", value, ORDINAL_SUFFIX))
    }

    /// Port of `Num2Word_MR.to_year`. The `longval=True` parameter is
    /// accepted and then ignored by Python, so the trait's arity matches.
    fn to_year(&self, value: &BigInt) -> Result<String> {
        if value.is_negative() {
            Ok(format!(
                "{}{}",
                YEAR_BC_PREFIX,
                self.to_cardinal(&value.abs())?
            ))
        } else {
            Ok(format!("{}{}", YEAR_PREFIX, self.to_cardinal(value)?))
        }
    }

    /// Port of `Num2Word_MR.to_cardinal`'s non-integer branch.
    ///
    /// MR does not use `Num2Word_Base.to_cardinal_float`; its overridden
    /// `to_cardinal` reads `str(number)` and speaks the fractional digits one
    /// at a time. So the raw f64 is turned back into `repr(float)` (or the
    /// Decimal into `str(Decimal)`) and fed to [`cardinal_from_str`] verbatim.
    /// The `precision=` kwarg (`precision_override`) is set on the converter by
    /// `__init__.py` but `to_cardinal` never reads `self.precision`, so it is
    /// ignored, exactly as Python ignores it.
    ///
    /// Python wraps both return arms in `.strip()`; it is a no-op on every
    /// reachable output (the string always begins and ends with a word) but is
    /// applied here for fidelity.
    fn to_cardinal_float(
        &self,
        value: &FloatValue,
        precision_override: Option<u32>,
    ) -> Result<String> {
        let _ = precision_override;
        let n = match value {
            // Python's str(float); the raw f64 crossed the boundary so repr()
            // can be reproduced from the bits.
            FloatValue::Float { value, .. } => py_float_repr(*value),
            // Python's str(Decimal) — exact, never routed through f64.
            FloatValue::Decimal { value, .. } => py_decimal_str(value),
        };
        Ok(cardinal_from_str(&n)?.trim().to_string())
    }

    // ---- currency ----------------------------------------------------

    /// `self.__class__.__name__`, for the NotImplementedError message that
    /// the inherited `to_cheque` raises on an unknown code.
    fn lang_name(&self) -> &str {
        "Num2Word_MR"
    }

    /// `CURRENCY_FORMS[code]`.
    ///
    /// **Only the inherited `to_cheque` reaches this.** MR's own
    /// `to_currency` overrides the lookup with a `.get(currency, ...INR)`
    /// fallback and never consults this hook — see [`LangMr::to_currency`].
    /// `to_cheque` keeps the strict `self.CURRENCY_FORMS[currency]` subscript,
    /// so `None` here reproduces the `KeyError` that Python catches and
    /// re-raises as NotImplementedError.
    fn currency_forms(&self, code: &str) -> Option<&CurrencyForms> {
        self.currency_forms.get(code)
    }

    /// Port of `Num2Word_MR.to_currency`.
    ///
    /// MR replaces `Num2Word_Base.to_currency` wholesale with a string-slicing
    /// implementation, so **none** of the base currency machinery participates:
    /// no `parse_currency_parts`, no `pluralize`, no `_money_verbose` /
    /// `_cents_verbose` / `_cents_terse`, no `CURRENCY_PRECISION`, and no
    /// `adjective` support. Four consequences, all confirmed against the
    /// corpus:
    ///
    /// 1. **Unknown codes never raise.** `CURRENCY_FORMS.get(currency,
    ///    CURRENCY_FORMS["INR"])` silently falls back to rupees, so
    ///    `to_currency(2, "CHF")` is "दोन रुपये" and JPY/KWD/BHD/CNY/CHF all
    ///    render in रुपये/पैसे. This is why the `mr` currency corpus has zero
    ///    NotImplementedError rows while the cheque corpus has five.
    /// 2. **Precision is hardcoded to two decimal places** by the `[:2]`
    ///    slice, so the 3-decimal (KWD/BHD) and 0-decimal (JPY) currencies get
    ///    no special handling: `12.34 KWD` is "बारा रुपये आणि तीस चार पैसे",
    ///    not mils, and `12.34 JPY` still shows a cents segment.
    /// 3. **`adjective` is accepted and then ignored** — MR declares the
    ///    parameter but never reads it, and defines no `CURRENCY_ADJECTIVES`.
    /// 4. **Cents are dropped whenever `right == 0`**, including for floats.
    ///    `1.0` -> parts[1] is "0" -> `int("00")` -> 0 -> falsy -> "एक रुपया",
    ///    *not* "एक रुपया आणि शून्य पैसे" as `Num2Word_Base` would give. So
    ///    MR reaches the base's "int skips cents" outcome by a different
    ///    route: the `Int`/`Decimal` split still matters lexically (an int has
    ///    no "." at all, a float may carry non-zero cents), but for a
    ///    whole-valued float the two coincide.
    ///
    /// Python computes `parts = str(val).split(".")` *after* `val = abs(val)`,
    /// so the sign is detached before stringification. `CurrencyValue` carries
    /// the value parsed from that same `str(value)`, and `BigDecimal`'s
    /// `Display` round-trips it (verified: "1.0" stays "1.0", "0.5" stays
    /// "0.5" — the scale is preserved, so `int("00")` vs `int("50")` comes out
    /// right). See the port report for the one lexical form where this
    /// round-trip diverges.
    fn to_currency(
        &self,
        val: &CurrencyValue,
        currency: &str,
        cents: bool,
        separator: Option<&str>,
        adjective: bool,
    ) -> Result<String> {
        // Trait now hands us None when the caller omitted separator=;
        // resolve it to this language's own default before the ported body.
        let separator = separator.unwrap_or(self.default_separator());
        // Restore MR's own `separator=" आणि "` default; see SEPARATOR_UNSET.
        let separator = if separator == SEPARATOR_UNSET {
            SEPARATOR_DEFAULT
        } else {
            separator
        };
        // Declared by Python and never read. Named for the reader's benefit.
        let _ = adjective;

        // `if val < 0: is_negative = True; val = abs(val)`. BigDecimal's
        // is_negative() is false for "-0.0" (the sign is not carried on a zero
        // int_val), matching Python's `-0.0 < 0` being False.
        let is_negative = val.is_negative();

        // `str(val)` — the value stringified *after* the abs().
        let s = match val {
            CurrencyValue::Int(v) => {
                if is_negative {
                    v.abs().to_string()
                } else {
                    v.to_string()
                }
            }
            CurrencyValue::Decimal { value: d, .. } => {
                if is_negative {
                    d.abs().to_string()
                } else {
                    d.to_string()
                }
            }
        };

        // `parts = str(val).split(".")` — split on every dot, as Python does.
        let parts: Vec<&str> = s.split('.').collect();

        // `left = int(parts[0]) if parts[0] else 0`. A non-numeric parts[0]
        // is a ValueError out of int(); str() of a float big enough to render
        // in exponent form ("1e+16") lands here and Python raises ValueError
        // too, so the variant matches rather than approximates.
        let left = if parts[0].is_empty() {
            BigInt::zero()
        } else {
            parts[0]
                .parse::<BigInt>()
                .map_err(|e| N2WError::Value(e.to_string()))?
        };

        // `right = int(parts[1][:2].ljust(2, "0")) if len(parts) > 1 and parts[1] else 0`
        // — a *truncating* two-character slice, not a rounding quantize, so
        // 12.349 gives 34 and 12.999 gives 99 (never 100). Sliced by chars().
        let right = if parts.len() > 1 && !parts[1].is_empty() {
            let mut frac: String = parts[1].chars().take(2).collect();
            while frac.chars().count() < 2 {
                frac.push('0');
            }
            frac.parse::<BigInt>()
                .map_err(|e| N2WError::Value(e.to_string()))?
        } else {
            BigInt::zero()
        };

        // `cr1, cr2 = self.CURRENCY_FORMS.get(currency, self.CURRENCY_FORMS["INR"])`
        let forms = match self.currency_forms.get(currency) {
            Some(f) => f,
            None => self
                .currency_forms
                .get(CURRENCY_FALLBACK)
                .expect("INR is inserted by LangMr::new"),
        };
        let cr1 = &forms.unit;
        let cr2 = &forms.subunit;

        let one = bi(1);

        // `result = left_str + " " + (cr1[1] if left != 1 else cr1[0])`
        let left_str = int_to_word(&left);
        let mut result = format!(
            "{} {}",
            left_str,
            if left != one { &cr1[1] } else { &cr1[0] }
        );

        // `if cents and right:` — `right` is falsy at 0, which is how a whole
        // float loses its cents segment.
        if cents && !right.is_zero() {
            let cents_str = int_to_word(&right);
            result.push_str(separator);
            result.push_str(&cents_str);
            result.push(' ');
            result.push_str(if right != one { &cr2[1] } else { &cr2[0] });
        }

        // `result = self.negword + result` — negword already carries its own
        // trailing space ("ऋण "), so this is the interior separator too.
        if is_negative {
            result = format!("{}{}", NEGWORD, result);
        }

        // Python's `.strip()`. Nothing here is ever space-padded (negword's
        // space lands mid-string), so this cannot bite — reproduced anyway.
        Ok(result.trim().to_string())
    }
}