iso6093 1.0.0

ISO 6093 numerical value parsing and printing
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
#![doc = include_str!("../README.md")]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::borrow::ToOwned;

/// An error that can occur while parsing ISO 6093 numbers
#[derive(PartialEq, Eq, Debug)]
pub struct ISO6093Error;

// An ISO 6093 Real Number
#[derive(Debug, PartialEq)]
pub enum ISO6093RealNumber {
    // NR1 Format
    // This is only able to represent integer values
    NR1(f64),

    // NR2 Format
    NR2(f64),

    // NR3 Format
    NR3(f64),
}

/// Parse an ISO 6093 NR1 format number (integer)
///
/// NR1 is a signed or unsigned integer:
/// - Optional leading spaces and zeroes
/// - Optional leading sign (+ or -)
/// - Sequence of decimal digits
///
/// Examples: "123", "-456", "+789", "  42", "0042"
pub fn parse_nr1(mut input: &str) -> Result<f64, ISO6093Error> {
    input = input.trim_start_matches(|c| c == ' ');
    input
        .parse::<i64>()
        .map(|v| v as f64)
        .map_err(|_| ISO6093Error)
}

/// Parse an ISO 6093 NR2 format number (decimal notation)
///
/// NR2 is a signed or unsigned fixed-point number:
/// - Optional leading sign (+ or -)
/// - Sequence of decimal digits with a decimal point
/// - The decimal point must be present
///
/// Examples: "123.45", "-0.789", "+12.3"
fn parse_nr2_ex(mut input: &str) -> Result<f64, ISO6093Error> {
    input = input.trim_start_matches(|c| c == ' ');
    // .5 Allowed by Rust, but not by NR2.
    if input.chars().next().is_some_and(|c| c == '.') {
        return Err(ISO6093Error);
    }
    let mut chars = input.chars();
    // +.5 and -.5 Allowed by Rust, but not by NR2.
    if chars.next().is_some_and(|c| c == '+' || c == '-') {
        if chars.next().is_some_and(|c| c == '.') {
            return Err(ISO6093Error);
        }
    }
    let mut has_decimal = false;
    let mut maybe_comma_index: Option<usize> = None;
    for (i, c) in input.char_indices() {
        if !c.is_ascii() {
            return Err(ISO6093Error);
        }
        if c == ',' {
            if has_decimal {
                return Err(ISO6093Error);
            }
            has_decimal = true;
            maybe_comma_index = Some(i);
        } else if c == '.' {
            if has_decimal {
                return Err(ISO6093Error);
            }
            has_decimal = true;
        } else if c.to_ascii_lowercase() == 'e' {
            return Err(ISO6093Error);
        }
    }

    if !has_decimal {
        return Err(ISO6093Error);
    }

    if let Some(comma_index) = maybe_comma_index {
        #[cfg(feature = "alloc")]
        {
            let mut normalized = input.to_owned();
            unsafe {
                normalized.as_bytes_mut()[comma_index] = b'.';
            }
            return normalized.parse::<f64>().map_err(|_| ISO6093Error);
        }
        #[cfg(not(feature = "alloc"))]
        {
            unreachable!();
        }
    } else {
        return input.parse::<f64>().map_err(|_| ISO6093Error);
    }
}

/// Parse an ISO 6093 NR3 format number (scientific notation)
///
/// NR3 is a signed or unsigned number in scientific notation:
/// - Optional leading sign (+ or -)
/// - Sequence of decimal digits with a decimal point
/// - The letter 'E' or 'e'
/// - Optional sign for the exponent (+ or -)
/// - Sequence of decimal digits for the exponent
///
/// Examples: "1.23E+45", "-6.78e-9", "+1.0E2"
fn parse_nr3_ex(mut input: &str) -> Result<f64, ISO6093Error> {
    input = input.trim_start_matches(|c| c == ' ');
    let mut has_exponent = false;
    let mut maybe_comma_index: Option<usize> = None;
    let mut has_decimal = false;
    for (i, c) in input.char_indices() {
        if !c.is_ascii() {
            return Err(ISO6093Error);
        }
        if c == '.' || c == ',' {
            if has_decimal {
                // duplicate
                return Err(ISO6093Error);
            }
            if has_exponent {
                // decimal after exponent
                return Err(ISO6093Error);
            }
            has_decimal = true;
            if c == ',' {
                maybe_comma_index = Some(i);
            }
        } else if c == 'E' || c == 'e' {
            has_exponent = true;
        }
    }

    if !has_exponent || !has_decimal {
        return Err(ISO6093Error);
    }

    if let Some(comma_index) = maybe_comma_index {
        #[cfg(feature = "alloc")]
        {
            let mut normalized = input.to_owned();
            unsafe {
                normalized.as_bytes_mut()[comma_index] = b'.';
            }
            return normalized.parse::<f64>().map_err(|_| ISO6093Error);
        }
        #[cfg(not(feature = "alloc"))]
        {
            unreachable!();
        }
    } else {
        return input.parse::<f64>().map_err(|_| ISO6093Error);
    }
}

/// Parse any ISO 6093 Numerical Value
///
/// This implementation does not call [parse_nr3] or [parse_nr2] directly, but
/// instead implements its own parsing logic that is mostly duplicate; the
/// rationale for this is to avoid duplicate iteration over all the characters
/// in the string. We need one pass over all characters to "categorize" the
/// string as NR1, NR2, or NR3. In that first pass, we can also do everything
/// that would have to do in [parse_nr3] or [parse_nr2] as well.
fn parse_iso6093_ex(mut input: &str) -> Result<ISO6093RealNumber, ISO6093Error> {
    input = input.trim_start_matches(|c| c == ' ');
    // .5 Allowed by Rust, but not by NR2.
    if input.chars().next().is_some_and(|c| c == '.') {
        return Err(ISO6093Error);
    }
    let mut chars = input.chars();
    // +.5 and -.5 Allowed by Rust, but not by NR2.
    if chars.next().is_some_and(|c| c == '+' || c == '-') {
        if chars.next().is_some_and(|c| c == '.') {
            return Err(ISO6093Error);
        }
    }

    let mut has_exponent = false;
    let mut has_decimal = false;
    let mut maybe_comma_index: Option<usize> = None;
    for (i, c) in input.char_indices() {
        if !c.is_ascii() {
            return Err(ISO6093Error);
        }
        if c == '.' || c == ',' {
            if has_decimal {
                // duplicate
                return Err(ISO6093Error);
            }
            if has_exponent {
                // decimal after exponent
                return Err(ISO6093Error);
            }
            has_decimal = true;
            if c == ',' {
                maybe_comma_index = Some(i);
            }
        } else if c == 'E' || c == 'e' {
            has_exponent = true;
        }
    }
    if !has_decimal {
        // This doesn't iterate over characters, so it has little overhead.
        return parse_nr1(input).map(|v| ISO6093RealNumber::NR1(v));
    }

    if let Some(comma_index) = maybe_comma_index {
        #[cfg(feature = "alloc")]
        {
            let mut normalized = input.to_owned();
            unsafe {
                normalized.as_bytes_mut()[comma_index] = b'.';
            }
            return normalized
                .parse::<f64>()
                .map(|v| {
                    if has_exponent {
                        ISO6093RealNumber::NR3(v)
                    } else {
                        ISO6093RealNumber::NR2(v)
                    }
                })
                .map_err(|_| ISO6093Error);
        }
        #[cfg(not(feature = "alloc"))]
        {
            unreachable!();
        }
    } else {
        return input
            .parse::<f64>()
            .map(|v| {
                if has_exponent {
                    ISO6093RealNumber::NR3(v)
                } else {
                    ISO6093RealNumber::NR2(v)
                }
            })
            .map_err(|_| ISO6093Error);
    }
}

/// Parse an ISO 6093 NR2 format number (decimal notation)
///
/// NR2 is a signed or unsigned fixed-point number:
/// - Optional leading sign (+ or -)
/// - Sequence of decimal digits with a decimal point
/// - The decimal point must be present
///
/// Examples: "123.45", "-0.789", "+12.3"
#[cfg(feature = "alloc")]
#[inline(always)]
pub fn parse_nr2(input: &str) -> Result<f64, ISO6093Error> {
    parse_nr2_ex(input)
}

/// Parse an ISO 6093 NR3 format number (scientific notation)
///
/// NR3 is a signed or unsigned number in scientific notation:
/// - Optional leading sign (+ or -)
/// - Sequence of decimal digits with a decimal point
/// - The letter 'E' or 'e'
/// - Optional sign for the exponent (+ or -)
/// - Sequence of decimal digits for the exponent
///
/// Examples: "1.23E+45", "-6.78e-9", "+1.0E2"
#[cfg(feature = "alloc")]
#[inline(always)]
pub fn parse_nr3(input: &str) -> Result<f64, ISO6093Error> {
    parse_nr3_ex(input)
}

/// Parse any ISO 6093 Numerical Value
///
/// This implementation does not call [parse_nr3] or [parse_nr2] directly, but
/// instead implements its own parsing logic that is mostly duplicate; the
/// rationale for this is to avoid duplicate iteration over all the characters
/// in the string. We need one pass over all characters to "categorize" the
/// string as NR1, NR2, or NR3. In that first pass, we can also do everything
/// that would have to do in [parse_nr3] or [parse_nr2] as well.
#[cfg(feature = "alloc")]
#[inline(always)]
pub fn parse_iso6093(input: &str) -> Result<ISO6093RealNumber, ISO6093Error> {
    parse_iso6093_ex(input)
}

/// Parse an ISO 6093 NR2 format number (decimal notation)
///
/// NR2 is a signed or unsigned fixed-point number:
/// - Optional leading sign (+ or -)
/// - Sequence of decimal digits with a decimal point
/// - The decimal point must be present
///
/// Examples: "123.45", "-0.789", "+12.3"
#[cfg(not(feature = "alloc"))]
pub fn parse_nr2(input: &mut str) -> Result<f64, ISO6093Error> {
    // This loop replaces just the first comma with a period.
    for (i, c) in input.char_indices() {
        if !c.is_ascii() {
            return Err(ISO6093Error);
        }
        if c == ',' {
            unsafe {
                input.as_bytes_mut()[i] = b'.';
            }
            // In the "deeper" functions, we check for duplicate decimals, so
            // we only need to replace one comma.
            break;
        }
    }
    parse_nr2_ex(input)
}

/// Parse an ISO 6093 NR3 format number (scientific notation)
///
/// NR3 is a signed or unsigned number in scientific notation:
/// - Optional leading sign (+ or -)
/// - Sequence of decimal digits with a decimal point
/// - The letter 'E' or 'e'
/// - Optional sign for the exponent (+ or -)
/// - Sequence of decimal digits for the exponent
///
/// Examples: "1.23E+45", "-6.78e-9", "+1.0E2"
#[cfg(not(feature = "alloc"))]
pub fn parse_nr3(input: &mut str) -> Result<f64, ISO6093Error> {
    // This loop replaces just the first comma with a period.
    for (i, c) in input.char_indices() {
        if !c.is_ascii() {
            return Err(ISO6093Error);
        }
        if c == ',' {
            unsafe {
                input.as_bytes_mut()[i] = b'.';
            }
            // In the "deeper" functions, we check for duplicate decimals, so
            // we only need to replace one comma.
            break;
        }
    }
    parse_nr3_ex(input)
}

/// Parse any ISO 6093 Numerical Value
///
/// This implementation does not call [parse_nr3] or [parse_nr2] directly, but
/// instead implements its own parsing logic that is mostly duplicate; the
/// rationale for this is to avoid duplicate iteration over all the characters
/// in the string. We need one pass over all characters to "categorize" the
/// string as NR1, NR2, or NR3. In that first pass, we can also do everything
/// that would have to do in [parse_nr3] or [parse_nr2] as well.
#[cfg(not(feature = "alloc"))]
pub fn parse_iso6093(input: &mut str) -> Result<ISO6093RealNumber, ISO6093Error> {
    for (i, c) in input.char_indices() {
        if !c.is_ascii() {
            return Err(ISO6093Error);
        }
        if c == ',' {
            unsafe {
                input.as_bytes_mut()[i] = b'.';
            }
            // In the "deeper" functions, we check for duplicate decimals, so
            // we only need to replace one comma.
            break;
        }
    }
    parse_iso6093_ex(input)
}

/// Format a number using ISO 6093 First Numerical Representation (NR1)
#[inline]
pub fn fmt_nr1(num: f64, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    let inum: i64 = num as i64;
    write!(f, "{}", inum)
}

/// Format a number using ISO 6093 Second Numerical Representation (NR2)
///
/// This is marked as `unsafe` because floating point numbers can be printed
/// using scientific notation (which is invalid NR2 form) if they are "too big"
/// or "too small," and there is absolutely no way I can predict this or
/// circumvent it. There is no way to force fixed-point notation in Rust.
pub unsafe fn fmt_nr2(num: f64, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    /* Typically, you check if it is an integer by checking if .fract() == 0.0,
    but fract() is not available in no-std crates. Further, it is already
    accepted that this is an unsafe function that might not do what you expect. */
    if num.is_finite() && (num == (num as i64 as f64)) {
        let inum: i64 = num as i64;
        return write!(f, "{}.", inum); // Format it with a guaranteed terminal decimal.
    }
    write!(f, "{}", num)
}

/// Format a number using ISO 6093 Third Numerical Representation (NR3)
#[inline]
pub fn fmt_nr3(num: f64, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(f, "{:e}", num)
}

impl core::fmt::Display for ISO6093RealNumber {
    /// Note that NR2 may be printed like NR3. See the documentation on
    /// [fmt_nr2].
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ISO6093RealNumber::NR1(v) => fmt_nr1(*v, f),
            ISO6093RealNumber::NR2(v) => unsafe { fmt_nr2(*v, f) },
            ISO6093RealNumber::NR3(v) => fmt_nr3(*v, f),
        }
    }
}

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

    extern crate alloc;
    use alloc::string::ToString;

    #[cfg(feature = "alloc")]
    macro_rules! s {
        ($arg:expr) => {
            $arg
        };
    }

    #[cfg(not(feature = "alloc"))]
    macro_rules! s {
        ($arg:expr) => {
            alloc::string::ToString::to_string($arg).as_mut_str()
        };
    }

    #[test]
    fn test_parse_nr1() {
        // Unsigned examples directly from ISO 6093
        assert_eq!(parse_nr1(s!("0004902")), Ok(4902.0));
        assert_eq!(parse_nr1(s!("  4902")), Ok(4902.0));
        assert_eq!(parse_nr1(s!("   4902")), Ok(4902.0));
        assert_eq!(parse_nr1(s!("0001234")), Ok(1234.0));
        assert_eq!(parse_nr1(s!("   1234")), Ok(1234.0));
        assert_eq!(parse_nr1(s!("0000000")), Ok(0.0));
        assert_eq!(parse_nr1(s!("      0")), Ok(0.0));
        assert_eq!(parse_nr1(s!("1234567")), Ok(1234567.0));

        // Signed examples directly from ISO 6093
        assert_eq!(parse_nr1(s!("+004902")), Ok(4902.0));
        assert_eq!(parse_nr1(s!(" +4902")), Ok(4902.0));
        assert_eq!(parse_nr1(s!("  +4902")), Ok(4902.0));
        assert_eq!(parse_nr1(s!("   4902")), Ok(4902.0));
        assert_eq!(parse_nr1(s!("+001234")), Ok(1234.0));
        assert_eq!(parse_nr1(s!("  +1234")), Ok(1234.0));
        assert_eq!(parse_nr1(s!("   1234")), Ok(1234.0));
        assert_eq!(parse_nr1(s!("-56780")), Ok(-56780.0));
        assert_eq!(parse_nr1(s!(" -56780")), Ok(-56780.0));
        assert_eq!(parse_nr1(s!("+000000")), Ok(0.0));
        assert_eq!(parse_nr1(s!("     +0")), Ok(0.0));
        assert_eq!(parse_nr1(s!("      0")), Ok(0.0));

        // Other tests
        assert_eq!(parse_nr1(s!("123")), Ok(123.0));
        assert_eq!(parse_nr1(s!("-456")), Ok(-456.0));
        assert_eq!(parse_nr1(s!("+789")), Ok(789.0));
        assert_eq!(parse_nr1(s!("")), Err(ISO6093Error));
        assert_eq!(parse_nr1(s!("12.3")), Err(ISO6093Error));
        assert_eq!(parse_nr1(s!("12E3")), Err(ISO6093Error));
    }

    #[test]
    fn test_parse_nr2() {
        // Unsigned examples directly from ISO 6093
        assert_eq!(parse_nr2(s!("1327.000")), Ok(1327.0));
        assert_eq!(parse_nr2(s!("0001327.")), Ok(1327.0));
        assert_eq!(parse_nr2(s!("   1327.")), Ok(1327.0));
        assert_eq!(parse_nr2(s!("00123.45")), Ok(123.45));
        assert_eq!(parse_nr2(s!("  123.45")), Ok(123.45));
        assert_eq!(parse_nr2(s!("  1237.0")), Ok(1237.0));
        assert_eq!(parse_nr2(s!("00.00001")), Ok(0.00001));
        assert_eq!(parse_nr2(s!("1234,567")), Ok(1234.567));
        assert_eq!(parse_nr2(s!("000,0000")), Ok(0.0));
        assert_eq!(parse_nr2(s!("     0,0")), Ok(0.0));

        // Signed examples directly from ISO 6093
        assert_eq!(parse_nr2(s!("+1327.00")), Ok(1327.00));
        assert_eq!(parse_nr2(s!("  +1327.")), Ok(1327.0));
        assert_eq!(parse_nr2(s!("   1327.")), Ok(1327.0));
        assert_eq!(parse_nr2(s!(" +123.45")), Ok(123.45));
        assert_eq!(parse_nr2(s!("  123,45")), Ok(123.45));
        assert_eq!(parse_nr2(s!(" +1237.0")), Ok(1237.0));
        assert_eq!(parse_nr2(s!("  1237,0")), Ok(1237.0));
        assert_eq!(parse_nr2(s!("+0.00001")), Ok(0.00001));
        assert_eq!(parse_nr2(s!("-5,67800")), Ok(-5.67800));
        assert_eq!(parse_nr2(s!("-05,6780")), Ok(-5.6780));
        assert_eq!(parse_nr2(s!("+0.00000")), Ok(0.0));
        assert_eq!(parse_nr2(s!("    +0,0")), Ok(0.0));
        assert_eq!(parse_nr2(s!("     0,0")), Ok(0.0));
        assert_eq!(parse_nr2(s!("      0,")), Ok(0.0));

        // Other tests
        assert_eq!(parse_nr2(s!("123.45")), Ok(123.45));
        assert_eq!(parse_nr2(s!("-67.89")), Ok(-67.89));
        assert_eq!(parse_nr2(s!("+0.123")), Ok(0.123));
        assert_eq!(parse_nr2(s!("")), Err(ISO6093Error));
        assert_eq!(parse_nr2(s!("123")), Err(ISO6093Error));
        assert_eq!(parse_nr2(s!("12E3")), Err(ISO6093Error));
    }

    #[test]
    fn test_parse_nr3() {
        // Examples directly from ISO 6093
        assert_eq!(parse_nr3(s!("+0,56E+4")), Ok(5600.0));
        assert_eq!(parse_nr3(s!("+5.6e+03")), Ok(5600.0));
        assert_eq!(parse_nr3(s!("+0,3E-04")), Ok(0.00003));
        assert_eq!(parse_nr3(s!(" 0,3e-04")), Ok(0.00003));
        assert_eq!(parse_nr3(s!("-2,8E+00")), Ok(-2.8));
        assert_eq!(parse_nr3(s!("+0,0E+00")), Ok(0.0));
        assert_eq!(parse_nr3(s!("   0.e+0")), Ok(0.0));

        // Other tests
        assert_eq!(parse_nr3(s!("1.23E+45")), Ok(1.23E+45));
        assert_eq!(parse_nr3(s!("-6.78e-9")), Ok(-6.78e-9));
        assert_eq!(parse_nr3(s!("+1.0E2")), Ok(100.0));
        assert_eq!(parse_nr3(s!("")), Err(ISO6093Error));
        assert_eq!(parse_nr3(s!("123.45")), Err(ISO6093Error));
        assert_eq!(parse_nr3(s!("123")), Err(ISO6093Error));
    }

    #[test]
    fn test_parse_iso6093() {
        // Unsigned examples directly from ISO 6093
        assert_eq!(
            parse_iso6093(s!("0004902")),
            Ok(ISO6093RealNumber::NR1(4902.0))
        );
        assert_eq!(
            parse_iso6093(s!("  4902")),
            Ok(ISO6093RealNumber::NR1(4902.0))
        );
        assert_eq!(
            parse_iso6093(s!("   4902")),
            Ok(ISO6093RealNumber::NR1(4902.0))
        );
        assert_eq!(
            parse_iso6093(s!("0001234")),
            Ok(ISO6093RealNumber::NR1(1234.0))
        );
        assert_eq!(
            parse_iso6093(s!("   1234")),
            Ok(ISO6093RealNumber::NR1(1234.0))
        );
        assert_eq!(
            parse_iso6093(s!("0000000")),
            Ok(ISO6093RealNumber::NR1(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("      0")),
            Ok(ISO6093RealNumber::NR1(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("1234567")),
            Ok(ISO6093RealNumber::NR1(1234567.0))
        );

        // Signed examples directly from ISO 6093
        assert_eq!(
            parse_iso6093(s!("+004902")),
            Ok(ISO6093RealNumber::NR1(4902.0))
        );
        assert_eq!(
            parse_iso6093(s!(" +4902")),
            Ok(ISO6093RealNumber::NR1(4902.0))
        );
        assert_eq!(
            parse_iso6093(s!("  +4902")),
            Ok(ISO6093RealNumber::NR1(4902.0))
        );
        assert_eq!(
            parse_iso6093(s!("   4902")),
            Ok(ISO6093RealNumber::NR1(4902.0))
        );
        assert_eq!(
            parse_iso6093(s!("+001234")),
            Ok(ISO6093RealNumber::NR1(1234.0))
        );
        assert_eq!(
            parse_iso6093(s!("  +1234")),
            Ok(ISO6093RealNumber::NR1(1234.0))
        );
        assert_eq!(
            parse_iso6093(s!("   1234")),
            Ok(ISO6093RealNumber::NR1(1234.0))
        );
        assert_eq!(
            parse_iso6093(s!("-56780")),
            Ok(ISO6093RealNumber::NR1(-56780.0))
        );
        assert_eq!(
            parse_iso6093(s!(" -56780")),
            Ok(ISO6093RealNumber::NR1(-56780.0))
        );
        assert_eq!(
            parse_iso6093(s!("+000000")),
            Ok(ISO6093RealNumber::NR1(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("     +0")),
            Ok(ISO6093RealNumber::NR1(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("      0")),
            Ok(ISO6093RealNumber::NR1(0.0))
        );

        // Unsigned examples directly from ISO 6093
        assert_eq!(
            parse_iso6093(s!("1327.000")),
            Ok(ISO6093RealNumber::NR2(1327.0))
        );
        assert_eq!(
            parse_iso6093(s!("0001327.")),
            Ok(ISO6093RealNumber::NR2(1327.0))
        );
        assert_eq!(
            parse_iso6093(s!("   1327.")),
            Ok(ISO6093RealNumber::NR2(1327.0))
        );
        assert_eq!(
            parse_iso6093(s!("00123.45")),
            Ok(ISO6093RealNumber::NR2(123.45))
        );
        assert_eq!(
            parse_iso6093(s!("  123.45")),
            Ok(ISO6093RealNumber::NR2(123.45))
        );
        assert_eq!(
            parse_iso6093(s!("  1237.0")),
            Ok(ISO6093RealNumber::NR2(1237.0))
        );
        assert_eq!(
            parse_iso6093(s!("00.00001")),
            Ok(ISO6093RealNumber::NR2(0.00001))
        );
        assert_eq!(
            parse_iso6093(s!("1234,567")),
            Ok(ISO6093RealNumber::NR2(1234.567))
        );
        assert_eq!(
            parse_iso6093(s!("000,0000")),
            Ok(ISO6093RealNumber::NR2(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("     0,0")),
            Ok(ISO6093RealNumber::NR2(0.0))
        );

        // Signed examples directly from ISO 6093
        assert_eq!(
            parse_iso6093(s!("+1327.00")),
            Ok(ISO6093RealNumber::NR2(1327.00))
        );
        assert_eq!(
            parse_iso6093(s!("  +1327.")),
            Ok(ISO6093RealNumber::NR2(1327.0))
        );
        assert_eq!(
            parse_iso6093(s!("   1327.")),
            Ok(ISO6093RealNumber::NR2(1327.0))
        );
        assert_eq!(
            parse_iso6093(s!(" +123.45")),
            Ok(ISO6093RealNumber::NR2(123.45))
        );
        assert_eq!(
            parse_iso6093(s!("  123,45")),
            Ok(ISO6093RealNumber::NR2(123.45))
        );
        assert_eq!(
            parse_iso6093(s!(" +1237.0")),
            Ok(ISO6093RealNumber::NR2(1237.0))
        );
        assert_eq!(
            parse_iso6093(s!("  1237,0")),
            Ok(ISO6093RealNumber::NR2(1237.0))
        );
        assert_eq!(
            parse_iso6093(s!("+0.00001")),
            Ok(ISO6093RealNumber::NR2(0.00001))
        );
        assert_eq!(
            parse_iso6093(s!("-5,67800")),
            Ok(ISO6093RealNumber::NR2(-5.67800))
        );
        assert_eq!(
            parse_iso6093(s!("-05,6780")),
            Ok(ISO6093RealNumber::NR2(-5.6780))
        );
        assert_eq!(
            parse_iso6093(s!("+0.00000")),
            Ok(ISO6093RealNumber::NR2(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("    +0,0")),
            Ok(ISO6093RealNumber::NR2(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("     0,0")),
            Ok(ISO6093RealNumber::NR2(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("      0,")),
            Ok(ISO6093RealNumber::NR2(0.0))
        );

        // Examples directly from ISO 6093
        assert_eq!(
            parse_iso6093(s!("+0,56E+4")),
            Ok(ISO6093RealNumber::NR3(5600.0))
        );
        assert_eq!(
            parse_iso6093(s!("+5.6e+03")),
            Ok(ISO6093RealNumber::NR3(5600.0))
        );
        assert_eq!(
            parse_iso6093(s!("+0,3E-04")),
            Ok(ISO6093RealNumber::NR3(0.00003))
        );
        assert_eq!(
            parse_iso6093(s!(" 0,3e-04")),
            Ok(ISO6093RealNumber::NR3(0.00003))
        );
        assert_eq!(
            parse_iso6093(s!("-2,8E+00")),
            Ok(ISO6093RealNumber::NR3(-2.8))
        );
        assert_eq!(
            parse_iso6093(s!("+0,0E+00")),
            Ok(ISO6093RealNumber::NR3(0.0))
        );
        assert_eq!(
            parse_iso6093(s!("   0.e+0")),
            Ok(ISO6093RealNumber::NR3(0.0))
        );
    }

    #[test]
    fn test_print_nr1() {
        assert_eq!(ISO6093RealNumber::NR1(123.0).to_string().as_str(), "123");
        assert_eq!(ISO6093RealNumber::NR1(-123.0).to_string().as_str(), "-123");
        assert_eq!(ISO6093RealNumber::NR1(123.5).to_string().as_str(), "123");
    }

    #[test]
    fn test_print_nr2() {
        assert_eq!(ISO6093RealNumber::NR2(123.0).to_string().as_str(), "123.");
        assert_eq!(ISO6093RealNumber::NR2(-123.0).to_string().as_str(), "-123.");
        assert_eq!(ISO6093RealNumber::NR2(123.5).to_string().as_str(), "123.5");
        assert_eq!(
            ISO6093RealNumber::NR2(0.00123).to_string().as_str(),
            "0.00123"
        );
    }

    #[test]
    fn test_print_nr3() {
        assert_eq!(
            ISO6093RealNumber::NR3(12300.0).to_string().as_str(),
            "1.23e4"
        );
        assert_eq!(
            ISO6093RealNumber::NR3(-12300.0).to_string().as_str(),
            "-1.23e4"
        );
        assert_eq!(
            ISO6093RealNumber::NR3(12300.5).to_string().as_str(),
            "1.23005e4"
        );
        assert_eq!(
            ISO6093RealNumber::NR3(0.00123).to_string().as_str(),
            "1.23e-3"
        );
    }
}