fastnum/decimal/
unsigned.rs

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
mod doc;
mod extras;
mod impls;

use impls::decimal::consts::consts_impl;

pub(crate) mod math;
pub(crate) mod parse;
pub(crate) mod round;

use core::{cmp::Ordering, fmt};

use crate::{
    decimal::{format, math::DecimalResult, signed::Decimal, ParseError, RoundingMode, Sign},
    int::{math::div_rem, UInt},
};

/// # Unsigned Decimal
///
/// Generic unsigned N-bits decimal number.
/// Consists of N-bit big unsigned integer, paired with a 64-bit signed
/// integer scaling factor which determines the position of the decimal point.
#[derive(Copy, Clone)]
pub struct UnsignedDecimal<const N: usize> {
    /// Unsigned integer for significant digits of a decimal number.
    value: UInt<N>,

    /// A positive scale means a negative power of 10.
    scale: i64,
}

consts_impl!();

impl<const N: usize> UnsignedDecimal<N> {
    #[inline]
    pub(crate) const fn new(value: UInt<N>, scale: i64) -> Self {
        Self { value, scale }
    }

    /// Creates and initializes an unsigned decimal from string.
    ///
    /// # Panics
    ///
    /// This function will panic if `UnsignedDecimal<N>` can not be constructed
    /// from given string.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::{UD256, udec256};
    ///
    /// assert_eq!(UD256::parse_str("1.2345"), udec256!(1.2345));
    /// ```
    #[track_caller]
    #[inline]
    pub const fn parse_str(s: &str) -> Self {
        match Self::from_str(s) {
            Ok(n) => n,
            Err(e) => panic!("{}", e.description()),
        }
    }

    /// Creates and initializes a Decimal from string.
    #[track_caller]
    #[inline]
    pub const fn from_str(s: &str) -> Result<Self, ParseError> {
        parse::from_str(s)
    }

    /// Returns the internal big integer, representing the significant
    /// decimal digits of a `UnsignedDecimal`, including significant trailing
    /// zeros.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::{udec256, u256};
    ///
    /// let a = udec256!(123.45);
    /// assert_eq!(a.decimal_digits(), u256!(12345));
    ///
    /// let b = udec256!(1.0);
    /// assert_eq!(b.decimal_digits(), u256!(10));
    /// ```
    #[inline]
    pub const fn decimal_digits(&self) -> UInt<N> {
        self.value
    }

    /// Returns the count of digits in the non-scaled integer representation
    #[inline]
    pub const fn decimal_digits_count(&self) -> usize {
        if self.is_zero() {
            return 1;
        }
        self.value.ilog10() as usize + 1
    }

    /// Returns the scale of the `UnsignedDecimal`, the total number of
    /// digits to the right of the decimal point (including insignificant
    /// leading zeros).
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// let a = udec256!(12345);  // No fractional part
    /// let b = udec256!(123.45);  // Fractional part
    /// let c = udec256!(0.0000012345);  // Completely fractional part
    /// let d = udec256!(500000000);  // No fractional part
    /// let e = udec256!(5e9);  // Negative-fractional part
    ///
    /// assert_eq!(a.fractional_digits_count(), 0);
    /// assert_eq!(b.fractional_digits_count(), 2);
    /// assert_eq!(c.fractional_digits_count(), 10);
    /// assert_eq!(d.fractional_digits_count(), 0);
    /// assert_eq!(e.fractional_digits_count(), -9);
    /// ```
    #[inline]
    pub const fn fractional_digits_count(&self) -> i64 {
        self.scale
    }

    /// Initialize unsigned decimal with `1 * 10`<sup>exp</sup> value.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::{UD256, udec256};
    ///
    /// assert_eq!(UD256::from_scale(0), udec256!(1));
    /// assert_eq!(UD256::from_scale(-0), udec256!(1));
    /// assert_eq!(UD256::from_scale(-3), udec256!(0.001));
    /// assert_eq!(UD256::from_scale(3), udec256!(1000));
    /// ```
    #[inline]
    pub const fn from_scale(exp: i64) -> Self {
        Self::new(UInt::ONE, -exp)
    }

    /// __Normalize__ this unsigned decimal moving all significant trailing
    /// zeros into the exponent.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::{udec256, u256};
    ///
    /// let a = udec256!(1234500);
    /// assert_eq!(a.decimal_digits(), u256!(1234500));
    /// assert_eq!(a.fractional_digits_count(), 0);
    ///
    /// let b = a.normalized();
    /// assert_eq!(b.decimal_digits(), u256!(12345));
    /// assert_eq!(b.fractional_digits_count(), -2);
    /// ```
    #[must_use = doc::must_use_op!()]
    pub const fn normalized(mut self) -> Self {
        if self.value.is_zero() {
            self.scale = 0;
        } else {
            let mut value;
            let mut remainder;
            while !self.value.is_zero() && self.scale > i64::MIN {
                (value, remainder) = div_rem(self.value, UInt::TEN);
                if remainder.is_zero() {
                    self.value = value;
                    self.scale -= 1;
                } else {
                    break;
                }
            }
        }
        self
    }

    /// Invert sign of given decimal.
    #[inline]
    pub const fn neg(self) -> Decimal<N> {
        Decimal::new(self, Sign::Minus)
    }

    /// Tests for `self` and `other` values to be equal, and is used by `==`
    /// operator.
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn eq(&self, rhs: &Self) -> bool {
        // TODO: performance optimization & some extra checks when normalize is partial
        let a = self.normalized();
        let b = rhs.normalized();
        (a.scale == b.scale) && (a.value.eq(&b.value))
    }

    /// Compares and returns the maximum of two unsigned decimal values.
    ///
    /// Returns the second argument if the comparison determines them to be
    /// equal.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::{udec256};
    ///
    /// assert_eq!(udec256!(1).max(udec256!(2)), udec256!(2));
    /// assert_eq!(udec256!(2).max(udec256!(2)), udec256!(2));
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn max(self, other: Self) -> Self {
        match self.cmp(&other) {
            Ordering::Less | Ordering::Equal => other,
            _ => self,
        }
    }

    /// Compares and returns the minimum of two undecimal values.
    ///
    /// Returns the first argument if the comparison determines them to be
    /// equal.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// assert_eq!(udec256!(1).min(udec256!(2)), udec256!(1));
    /// assert_eq!(udec256!(2).min(udec256!(2)), udec256!(2));
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn min(self, other: Self) -> Self {
        match self.cmp(&other) {
            Ordering::Less | Ordering::Equal => self,
            _ => other,
        }
    }

    /// Restrict an unsigned decimal value to a certain interval.
    ///
    /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
    /// less than `min`. Otherwise, this returns `self`.
    ///
    /// # Panics
    ///
    /// Panics if `min > max`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// assert_eq!(udec256!(0).clamp(udec256!(3), udec256!(5)), udec256!(3));
    /// assert_eq!(udec256!(3).clamp(udec256!(1), udec256!(5)), udec256!(3));
    /// assert_eq!(udec256!(6).clamp(udec256!(1), udec256!(5)), udec256!(5));
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn clamp(self, min: Self, max: Self) -> Self {
        assert!(min.le(&max));
        if let Ordering::Less = self.cmp(&min) {
            min
        } else if let Ordering::Greater = self.cmp(&max) {
            max
        } else {
            self
        }
    }

    /// Tests unsigned decimal `self` less than `other` and is used by the `<`
    /// operator.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// assert_eq!(udec256!(1.0).lt(&udec256!(1.0)), false);
    /// assert_eq!(udec256!(1.0).lt(&udec256!(2.0)), true);
    /// assert_eq!(udec256!(2.0).lt(&udec256!(1.0)), false);
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn lt(&self, other: &Self) -> bool {
        #[allow(clippy::match_like_matches_macro)]
        match self.cmp(other) {
            Ordering::Less => true,
            _ => false,
        }
    }

    /// Tests unsigned decimal `self` less than or equal to `other` and is used
    /// by the `<=` operator.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// assert_eq!(udec256!(1.0).le(&udec256!(1.0)), true);
    /// assert_eq!(udec256!(1.0).le(&udec256!(2.0)), true);
    /// assert_eq!(udec256!(2.0).le(&udec256!(1.0)), false);
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn le(&self, other: &Self) -> bool {
        #[allow(clippy::match_like_matches_macro)]
        match self.cmp(other) {
            Ordering::Less | Ordering::Equal => true,
            _ => false,
        }
    }

    /// Tests unsigned decimal `self` greater than `other` and is used by the
    /// `>` operator.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// assert_eq!(udec256!(1.0).gt(&udec256!(1.0)), false);
    /// assert_eq!(udec256!(1.0).gt(&udec256!(2.0)), false);
    /// assert_eq!(udec256!(2.0).gt(&udec256!(1.0)), true);
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn gt(&self, other: &Self) -> bool {
        #[allow(clippy::match_like_matches_macro)]
        match self.cmp(other) {
            Ordering::Greater => true,
            _ => false,
        }
    }

    /// Tests unsigned decimal `self` greater than or equal to `other` and is
    /// used by the `>=` operator.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// assert_eq!(udec256!(1.0).ge(&udec256!(1.0)), true);
    /// assert_eq!(udec256!(1.0).ge(&udec256!(2.0)), false);
    /// assert_eq!(udec256!(2.0).ge(&udec256!(1.0)), true);
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn ge(&self, other: &Self) -> bool {
        #[allow(clippy::match_like_matches_macro)]
        match self.cmp(other) {
            Ordering::Greater | Ordering::Equal => true,
            _ => false,
        }
    }

    /// This method returns an [`Ordering`] between `self` and `other`.
    ///
    /// By convention, `self.cmp(&other)` returns the ordering matching the
    /// expression `self <operator> other` if true.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    /// use std::cmp::Ordering;
    ///
    /// assert_eq!(udec256!(5).cmp(&udec256!(10)), Ordering::Less);
    /// assert_eq!(udec256!(10).cmp(&udec256!(5)), Ordering::Greater);
    /// assert_eq!(udec256!(5).cmp(&udec256!(5)), Ordering::Equal);
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn cmp(&self, rhs: &Self) -> Ordering {
        match (self.is_zero(), rhs.is_zero()) {
            (true, true) => {
                return Ordering::Equal;
            }
            (true, false) => {
                return Ordering::Less;
            }
            (false, true) => {
                return Ordering::Greater;
            }
            (_, _) => {}
        }

        let a = self.normalized();
        let b = rhs.normalized();

        if a.scale == b.scale {
            return a.value.cmp(&b.value);
        }

        let a_exp = a.value.ilog10() as i64 - a.scale;
        let b_exp = b.value.ilog10() as i64 - b.scale;

        if a_exp == b_exp {
            if a.scale > b.scale {
                let (mul, false) = UInt::TEN.overflowing_pow((a.scale - b.scale) as u32) else {
                    return Ordering::Less;
                };

                let (value, false) = b.value.overflowing_mul(mul) else {
                    return Ordering::Less;
                };

                a.value.cmp(&value)
            } else {
                let (mul, false) = UInt::TEN.overflowing_pow((b.scale - a.scale) as u32) else {
                    return Ordering::Less;
                };

                let (value, false) = a.value.overflowing_mul(mul) else {
                    return Ordering::Less;
                };

                value.cmp(&b.value)
            }
        } else if a_exp > b_exp {
            Ordering::Greater
        } else {
            Ordering::Less
        }
    }

    /// Calculates `self` + `rhs`.
    ///
    /// Returns [DecimalResult] with result of addition and [emergency
    /// flags](crate#arithmetic-result). Is internally used by the `+`
    /// operator.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::ONE;
    /// let b = UD256::TWO;
    ///
    /// let c = a.add(b, RoundingMode::default()).unwrap();
    /// assert_eq!(c, udec256!(3));
    /// ```
    ///
    /// ```should_panic
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::MAX;
    /// let b = UD256::MAX;
    ///
    /// let c = a + b;
    /// ```
    ///
    /// For more information about flags and [crate::decimal::ArithmeticPolicy]
    /// see: [section](crate#arithmetic-result).
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn add(self, rhs: Self, rounding_mode: RoundingMode) -> DecimalResult<Self> {
        math::add(self, rhs, rounding_mode)
    }

    /// Calculates `self` - `rhs`.
    ///
    /// Returns [DecimalResult] with result of subtraction and [emergency
    /// flags](crate#arithmetic-result). Is internally used by the `-`
    /// operator.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::FIVE;
    /// let b = UD256::TWO;
    ///
    /// let c = a.sub(b, RoundingMode::default()).unwrap();
    /// assert_eq!(c, udec256!(3));
    /// ```
    ///
    /// ```should_panic
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::ZERO;
    /// let b = UD256::ONE;
    ///
    /// let c = a - b;
    /// ```
    ///
    /// For more information about flags and [crate::decimal::ArithmeticPolicy]
    /// see: [section](crate#arithmetic-result).
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn sub(self, rhs: Self, rounding_mode: RoundingMode) -> DecimalResult<Self> {
        math::sub(self, rhs, rounding_mode)
    }

    /// Calculates `self` × `rhs`.
    ///
    /// Returns [DecimalResult] with result of multiplication and [emergency
    /// flags](crate#arithmetic-result). Is internally used by the `*`
    /// operator.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::FIVE;
    /// let b = UD256::TWO;
    ///
    /// let c = a.mul(b, RoundingMode::default()).unwrap();
    /// assert_eq!(c, udec256!(10));
    /// ```
    ///
    /// ```should_panic
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::MAX;
    /// let b = UD256::MAX;
    ///
    /// let c = a * b;
    /// ```
    ///
    /// For more information about flags and [crate::decimal::ArithmeticPolicy]
    /// see: [section](crate#arithmetic-result).
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn mul(self, rhs: Self, rounding_mode: RoundingMode) -> DecimalResult<Self> {
        math::mul(self, rhs, rounding_mode)
    }

    /// Calculates `self` ÷ `rhs`.
    ///
    /// Returns [DecimalResult] with result of division and [emergency
    /// flags](crate#arithmetic-result). Is internally used by the `/`
    /// operator.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::FIVE;
    /// let b = UD256::TWO;
    ///
    /// let c = a.div(b, RoundingMode::default()).unwrap();
    /// assert_eq!(c, udec256!(2.5));
    /// ```
    ///
    /// ```should_panic
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::ONE;
    /// let b = UD256::ZERO;
    ///
    /// let c = a / b;
    /// ```
    ///
    /// For more information about flags and [crate::decimal::ArithmeticPolicy]
    /// see: [section](crate#arithmetic-result).
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn div(self, rhs: Self, rounding_mode: RoundingMode) -> DecimalResult<Self> {
        math::div(self, rhs, rounding_mode)
    }

    /// Calculates `self` % `rhs`.
    ///
    /// Returns [DecimalResult] with result of division reminder and [emergency
    /// flags](crate#arithmetic-result). Is internally used by the `%`
    /// operator.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use fastnum::{udec256, UD256};
    /// use fastnum::decimal::RoundingMode;
    ///
    /// let a = UD256::FIVE;
    /// let b = UD256::TWO;
    ///
    /// let c = a.rem(b, RoundingMode::default()).unwrap();
    /// assert_eq!(c, udec256!(1));
    /// ```
    /// For more information about flags and [crate::decimal::ArithmeticPolicy]
    /// see: [section](crate#arithmetic-result).
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn rem(self, rhs: Self, rounding_mode: RoundingMode) -> DecimalResult<Self> {
        math::rem(self, rhs, rounding_mode)
    }

    /// Return given decimal number rounded to 'digits' precision after the
    /// decimal point, using given [RoundingMode] unwrapped with default
    /// rounding and overflow policy.
    ///
    /// # Panics:
    ///
    /// This method will panic if round operation (up-scale or down-scale)
    /// performs with some emergency flags and specified
    /// [crate::decimal::ArithmeticPolicy] enjoin to panic when the
    /// corresponding flag occurs.
    ///
    /// For more information about flags and [crate::decimal::ArithmeticPolicy]
    /// see: [section](crate#arithmetic-result).
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::{udec256, decimal::RoundingMode};
    ///
    /// let n = udec256!(129.41675);
    ///
    /// assert_eq!(n.round(2, RoundingMode::Up),  udec256!(129.42));
    /// assert_eq!(n.round(-1, RoundingMode::Down),  udec256!(120));
    /// assert_eq!(n.round(4, RoundingMode::HalfEven),  udec256!(129.4168));
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn round(self, digits: i64, rounding_mode: RoundingMode) -> Self {
        self.with_scale(digits, rounding_mode).unwrap()
    }

    /// Returns [DecimalResult] with result of round given decimal number
    /// to 'digits' precision after the decimal point using given
    /// [RoundingMode].
    ///
    /// For more information about flags and [crate::decimal::ArithmeticPolicy]
    /// see: [section](crate#arithmetic-result).
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::{udec256, decimal::RoundingMode};
    ///
    /// let n = udec256!(129.41675);
    ///
    /// assert_eq!(n.with_scale(2, RoundingMode::Up).unwrap(),  udec256!(129.42));
    /// assert_eq!(n.with_scale(-1, RoundingMode::Down).unwrap(),  udec256!(120));
    /// assert_eq!(n.with_scale(4, RoundingMode::HalfEven).unwrap(),  udec256!(129.4168));
    /// ```
    #[must_use = doc::must_use_op!()]
    #[inline]
    pub const fn with_scale(
        self,
        new_scale: i64,
        rounding_mode: RoundingMode,
    ) -> DecimalResult<Self> {
        round::with_scale(self, new_scale, rounding_mode)
    }

    /// Create string of this unsigned decimal in scientific notation.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// let n = udec256!(12345678);
    /// assert_eq!(&n.to_scientific_notation(), "1.2345678e7");
    /// ```
    pub fn to_scientific_notation(&self) -> String {
        let mut output = String::new();
        self.write_scientific_notation(&mut output)
            .expect("Could not write to string");
        output
    }

    /// Create string of this unsigned decimal in engineering notation.
    ///
    /// Engineering notation is scientific notation with the exponent
    /// coerced to a multiple of three.
    ///
    /// # Examples
    ///
    /// ```
    /// use fastnum::udec256;
    ///
    /// let n = udec256!(12345678);
    /// assert_eq!(&n.to_engineering_notation(), "12.345678e6");
    /// ```
    pub fn to_engineering_notation(&self) -> String {
        let mut output = String::new();
        self.write_engineering_notation(&mut output)
            .expect("Could not write to string");
        output
    }
}

#[doc(hidden)]
impl<const N: usize> UnsignedDecimal<N> {
    #[inline]
    pub(crate) fn type_name() -> String {
        format!("UD{}", N * 64)
    }

    /// Write unsigned decimal in scientific notation to writer `w`.
    #[inline]
    pub(crate) fn write_scientific_notation<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
        if self.is_zero() {
            return w.write_str("0e0");
        }
        let digits = self.value.to_str_radix(10);
        let scale = self.scale;
        format::write_scientific_notation(digits, scale, w)
    }

    /// Write unsigned decimal in engineering notation to writer `w`.
    pub(crate) fn write_engineering_notation<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
        if self.is_zero() {
            return w.write_str("0e0");
        }
        let digits = self.value.to_str_radix(10);
        let scale = self.scale;
        format::write_engineering_notation(digits, scale, w)
    }
}