Debian Codegraph @

debian/rust-vendor/fixed_decimal/src/signed_decimal.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use core::fmt;
6 use core::ops::{Deref, DerefMut};
7 use core::str::FromStr;
8
9 use crate::uint_iterator::IntIterator;
10 use crate::{variations::Signed, UnsignedDecimal};
11 #[cfg(feature = "ryu")]
12 use crate::{FloatPrecision, LimitError};
13 use crate::{
14 IncrementLike, NoIncrement, ParseError, RoundingIncrement, Sign, SignDisplay,
15 SignedRoundingMode, UnsignedRoundingMode,
16 };
17
18 /// A Type containing a [`UnsignedDecimal`] and a [`Sign`] to represent a signed decimal number.
19 ///
20 /// **The primary definition of this type is in the [`fixed_decimal`](https://docs.rs/fixed_decimal) crate. Other ICU4X crates re-export it for convenience.**
21 ///
22 /// Supports a mantissa of non-zero digits and a number of leading and trailing
23 /// zeros, as well as an optional sign; used for formatting and plural selection.
24 ///
25 /// # Data Types
26 ///
27 /// The following types can be converted to a `Decimal`:
28 ///
29 /// - Integers, signed and unsigned
30 /// - Strings representing an arbitrary-precision decimal
31 /// - Floating point values (using the `ryu` feature)
32 ///
33 /// To create a [`Decimal`] with fractional digits, you have several options:
34 /// - Create it from an integer and then call [`UnsignedDecimal::multiply_pow10`] (you can also call `multiply_pow10` directly on the [`Decimal`]).
35 /// - Create it from a string.
36 /// - When the `ryu` feature is enabled, create it from a floating point value using [`Decimal::try_from_f64`].
37 ///
38 /// # Examples
39 ///
40 /// ```
41 /// use fixed_decimal::Decimal;
42 ///
43 /// let mut dec = Decimal::from(250);
44 /// assert_eq!("250", dec.to_string());
45 ///
46 /// dec.multiply_pow10(-2);
47 /// assert_eq!("2.50", dec.to_string());
48 /// ```
49 pub type Decimal = Signed<UnsignedDecimal>;
50
51 impl Decimal {
52 /// Creates a [`Decimal`] from parts.
53 pub fn new(sign: Sign, absolute: UnsignedDecimal) -> Self {
54 Decimal { sign, absolute }
55 }
56
57 /// Parses a [`Decimal`].
58 #[inline]
59 pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
60 Self::try_from_utf8(s.as_bytes())
61 }
62
63 /// See [`Self::try_from_str`]
64 pub fn try_from_utf8(input_str: &[u8]) -> Result<Self, ParseError> {
65 // input_str: the input string
66 // no_sign_str: the input string when the sign is removed from it
67 if input_str.is_empty() {
68 return Err(ParseError::Syntax);
69 }
70 #[expect(clippy::indexing_slicing)] // The string is not empty.
71 let sign = match input_str[0] {
72 b'-' => Sign::Negative,
73 b'+' => Sign::Positive,
74 _ => Sign::None,
75 };
76 #[expect(clippy::indexing_slicing)] // The string is not empty.
77 let no_sign_str = if sign == Sign::None {
78 input_str
79 } else {
80 &input_str[1..]
81 };
82 if no_sign_str.is_empty() {
83 return Err(ParseError::Syntax);
84 }
85
86 let unsigned_decimal = UnsignedDecimal::try_from_no_sign_utf8(no_sign_str)?;
87 Ok(Self {
88 sign,
89 absolute: unsigned_decimal,
90 })
91 }
92
93 // TODO: Shall we add this method to `Signed<T>`?
94 /// Sets the sign of this number according to the given sign display strategy.
95 ///
96 /// # Examples
97 /// ```
98 /// use fixed_decimal::Decimal;
99 /// use fixed_decimal::SignDisplay::*;
100 ///
101 /// let mut dec = Decimal::from(1729);
102 /// assert_eq!("1729", dec.to_string());
103 /// dec.apply_sign_display(Always);
104 /// assert_eq!("+1729", dec.to_string());
105 /// ```
106 pub fn apply_sign_display(&mut self, sign_display: SignDisplay) {
107 use Sign::*;
108 match sign_display {
109 SignDisplay::Auto => {
110 if self.sign != Negative {
111 self.sign = None
112 }
113 }
114 SignDisplay::Always => {
115 if self.sign != Negative {
116 self.sign = Positive
117 }
118 }
119 SignDisplay::Never => self.sign = None,
120 SignDisplay::ExceptZero => {
121 if self.absolute.is_zero() {
122 self.sign = None
123 } else if self.sign != Negative {
124 self.sign = Positive
125 }
126 }
127 SignDisplay::Negative => {
128 if self.sign != Negative || self.absolute.is_zero() {
129 self.sign = None
130 }
131 }
132 }
133 }
134
135 // TODO: Shall we add this method to `Signed<T>`?
136 /// Returns this number with its sign set according to the given sign display strategy.
137 ///
138 /// # Examples
139 /// ```
140 /// use fixed_decimal::Decimal;
141 /// use fixed_decimal::SignDisplay::*;
142 ///
143 /// assert_eq!(
144 /// "+1729",
145 /// Decimal::from(1729)
146 /// .with_sign_display(ExceptZero)
147 /// .to_string()
148 /// );
149 /// ```
150 pub fn with_sign_display(mut self, sign_display: SignDisplay) -> Self {
151 self.apply_sign_display(sign_display);
152 self
153 }
154 }
155
156 impl FromStr for Decimal {
157 type Err = ParseError;
158 fn from_str(s: &str) -> Result<Self, Self::Err> {
159 Self::try_from_str(s)
160 }
161 }
162
163 macro_rules! impl_from_signed_integer_type {
164 ($itype:ident, $utype: ident) => {
165 impl From<$itype> for Decimal {
166 fn from(value: $itype) -> Self {
167 let int_iterator: IntIterator<$utype> = value.into();
168 let sign = if int_iterator.is_negative {
169 Sign::Negative
170 } else {
171 Sign::None
172 };
173 let value = UnsignedDecimal::from_ascending(int_iterator)
174 .expect("All built-in integer types should fit");
175 Decimal {
176 sign,
177 absolute: value,
178 }
179 }
180 }
181 };
182 }
183
184 impl_from_signed_integer_type!(isize, usize);
185 impl_from_signed_integer_type!(i128, u128);
186 impl_from_signed_integer_type!(i64, u64);
187 impl_from_signed_integer_type!(i32, u32);
188 impl_from_signed_integer_type!(i16, u16);
189 impl_from_signed_integer_type!(i8, u8);
190
191 macro_rules! impl_from_unsigned_integer_type {
192 ($utype: ident) => {
193 impl From<$utype> for Decimal {
194 fn from(value: $utype) -> Self {
195 let int_iterator: IntIterator<$utype> = value.into();
196 Self {
197 sign: Sign::None,
198 absolute: UnsignedDecimal::from_ascending(int_iterator)
199 .expect("All built-in integer types should fit"),
200 }
201 }
202 }
203 };
204 }
205
206 impl_from_unsigned_integer_type!(usize);
207 impl_from_unsigned_integer_type!(u128);
208 impl_from_unsigned_integer_type!(u64);
209 impl_from_unsigned_integer_type!(u32);
210 impl_from_unsigned_integer_type!(u16);
211 impl_from_unsigned_integer_type!(u8);
212
213 #[cfg(feature = "ryu")]
214 impl Decimal {
215 /// Constructs a [`Decimal`] from an f64.
216 ///
217 /// Since f64 values do not carry a notion of their precision, the second argument to this
218 /// function specifies the type of precision associated with the f64. For more information,
219 /// see [`FloatPrecision`].
220 ///
221 /// This function uses `ryu`, which is an efficient double-to-string algorithm, but other
222 /// implementations may yield higher performance; for more details, see
223 /// [icu4x#166](https://github.com/unicode-org/icu4x/issues/166).
224 ///
225 /// This function can be made available with the `"ryu"` Cargo feature.
226 ///
227 /// ```rust
228 /// use fixed_decimal::{Decimal, FloatPrecision};
229 /// use writeable::assert_writeable_eq;
230 ///
231 /// let decimal = Decimal::try_from_f64(-5.1, FloatPrecision::Magnitude(-2))
232 /// .expect("Finite quantity with limited precision");
233 /// assert_writeable_eq!(decimal, "-5.10");
234 ///
235 /// let decimal = Decimal::try_from_f64(0.012345678, FloatPrecision::RoundTrip)
236 /// .expect("Finite quantity");
237 /// assert_writeable_eq!(decimal, "0.012345678");
238 ///
239 /// let decimal = Decimal::try_from_f64(12345678000., FloatPrecision::Integer)
240 /// .expect("Finite, integer-valued quantity");
241 /// assert_writeable_eq!(decimal, "12345678000");
242 /// ```
243 ///
244 /// Negative zero is supported.
245 ///
246 /// ```rust
247 /// use fixed_decimal::{Decimal, FloatPrecision};
248 /// use writeable::assert_writeable_eq;
249 ///
250 /// // IEEE 754 for floating point defines the sign bit separate
251 /// // from the mantissa and exponent, allowing for -0.
252 /// let negative_zero = Decimal::try_from_f64(-0.0, FloatPrecision::Integer)
253 /// .expect("Negative zero");
254 /// assert_writeable_eq!(negative_zero, "-0");
255 /// ```
256 pub fn try_from_f64(float: f64, precision: FloatPrecision) -> Result<Self, LimitError> {
257 match float.is_sign_negative() {
258 true => Ok(Decimal {
259 sign: Sign::Negative,
260 absolute: UnsignedDecimal::try_from_f64(-float, precision)?,
261 }),
262 false => Ok(Decimal {
263 sign: Sign::None,
264 absolute: UnsignedDecimal::try_from_f64(float, precision)?,
265 }),
266 }
267 }
268 }
269
270 impl Deref for Decimal {
271 type Target = UnsignedDecimal;
272 fn deref(&self) -> &Self::Target {
273 &self.absolute
274 }
275 }
276
277 impl DerefMut for Decimal {
278 fn deref_mut(&mut self) -> &mut Self::Target {
279 &mut self.absolute
280 }
281 }
282
283 /// All the rounding and rounding related logic is implmented in this implmentation block.
284 impl Decimal {
285 /// Rounds this number at a particular digit position.
286 ///
287 /// This uses half to even rounding, which rounds to the nearest integer and resolves ties by
288 /// selecting the nearest even integer to the original value.
289 ///
290 /// # Examples
291 ///
292 /// ```
293 /// use fixed_decimal::Decimal;
294 /// # use std::str::FromStr;
295 ///
296 /// let mut dec = Decimal::from_str("-1.5").unwrap();
297 /// dec.round(0);
298 /// assert_eq!("-2", dec.to_string());
299 /// let mut dec = Decimal::from_str("0.4").unwrap();
300 /// dec.round(0);
301 /// assert_eq!("0", dec.to_string());
302 /// let mut dec = Decimal::from_str("0.5").unwrap();
303 /// dec.round(0);
304 /// assert_eq!("0", dec.to_string());
305 /// let mut dec = Decimal::from_str("0.6").unwrap();
306 /// dec.round(0);
307 /// assert_eq!("1", dec.to_string());
308 /// let mut dec = Decimal::from_str("1.5").unwrap();
309 /// dec.round(0);
310 /// assert_eq!("2", dec.to_string());
311 /// ```
312 pub fn round(&mut self, position: i16) {
313 self.half_even_to_increment_internal(position, NoIncrement)
314 }
315
316 /// Returns this number rounded at a particular digit position.
317 ///
318 /// This uses half to even rounding by default, which rounds to the nearest integer and
319 /// resolves ties by selecting the nearest even integer to the original value.
320 ///
321 /// # Examples
322 ///
323 /// ```
324 /// use fixed_decimal::Decimal;
325 /// # use std::str::FromStr;
326 ///
327 /// let mut dec = Decimal::from_str("-1.5").unwrap();
328 /// assert_eq!("-2", dec.rounded(0).to_string());
329 /// let mut dec = Decimal::from_str("0.4").unwrap();
330 /// assert_eq!("0", dec.rounded(0).to_string());
331 /// let mut dec = Decimal::from_str("0.5").unwrap();
332 /// assert_eq!("0", dec.rounded(0).to_string());
333 /// let mut dec = Decimal::from_str("0.6").unwrap();
334 /// assert_eq!("1", dec.rounded(0).to_string());
335 /// let mut dec = Decimal::from_str("1.5").unwrap();
336 /// assert_eq!("2", dec.rounded(0).to_string());
337 /// ```
338 pub fn rounded(mut self, position: i16) -> Self {
339 self.round(position);
340 self
341 }
342
343 /// Rounds this number towards positive infinity at a particular digit position.
344 ///
345 /// # Examples
346 ///
347 /// ```
348 /// use fixed_decimal::Decimal;
349 /// # use std::str::FromStr;
350 ///
351 /// let mut dec = Decimal::from_str("-1.5").unwrap();
352 /// dec.ceil(0);
353 /// assert_eq!("-1", dec.to_string());
354 /// let mut dec = Decimal::from_str("0.4").unwrap();
355 /// dec.ceil(0);
356 /// assert_eq!("1", dec.to_string());
357 /// let mut dec = Decimal::from_str("0.5").unwrap();
358 /// dec.ceil(0);
359 /// assert_eq!("1", dec.to_string());
360 /// let mut dec = Decimal::from_str("0.6").unwrap();
361 /// dec.ceil(0);
362 /// assert_eq!("1", dec.to_string());
363 /// let mut dec = Decimal::from_str("1.5").unwrap();
364 /// dec.ceil(0);
365 /// assert_eq!("2", dec.to_string());
366 /// ```
367 #[inline(never)]
368 pub fn ceil(&mut self, position: i16) {
369 self.ceil_to_increment_internal(position, NoIncrement);
370 }
371
372 /// Returns this number rounded towards positive infinity at a particular digit position.
373 ///
374 /// # Examples
375 ///
376 /// ```
377 /// use fixed_decimal::Decimal;
378 /// # use std::str::FromStr;
379 ///
380 /// let dec = Decimal::from_str("-1.5").unwrap();
381 /// assert_eq!("-1", dec.ceiled(0).to_string());
382 /// let dec = Decimal::from_str("0.4").unwrap();
383 /// assert_eq!("1", dec.ceiled(0).to_string());
384 /// let dec = Decimal::from_str("0.5").unwrap();
385 /// assert_eq!("1", dec.ceiled(0).to_string());
386 /// let dec = Decimal::from_str("0.6").unwrap();
387 /// assert_eq!("1", dec.ceiled(0).to_string());
388 /// let dec = Decimal::from_str("1.5").unwrap();
389 /// assert_eq!("2", dec.ceiled(0).to_string());
390 /// ```
391 pub fn ceiled(mut self, position: i16) -> Self {
392 self.ceil(position);
393 self
394 }
395
396 /// Rounds this number away from zero at a particular digit position.
397 ///
398 /// # Examples
399 ///
400 /// ```
401 /// use fixed_decimal::Decimal;
402 /// # use std::str::FromStr;
403 ///
404 /// let mut dec = Decimal::from_str("-1.5").unwrap();
405 /// dec.expand(0);
406 /// assert_eq!("-2", dec.to_string());
407 /// let mut dec = Decimal::from_str("0.4").unwrap();
408 /// dec.expand(0);
409 /// assert_eq!("1", dec.to_string());
410 /// let mut dec = Decimal::from_str("0.5").unwrap();
411 /// dec.expand(0);
412 /// assert_eq!("1", dec.to_string());
413 /// let mut dec = Decimal::from_str("0.6").unwrap();
414 /// dec.expand(0);
415 /// assert_eq!("1", dec.to_string());
416 /// let mut dec = Decimal::from_str("1.5").unwrap();
417 /// dec.expand(0);
418 /// assert_eq!("2", dec.to_string());
419 /// ```
420 #[inline(never)]
421 pub fn expand(&mut self, position: i16) {
422 self.expand_to_increment_internal(position, NoIncrement)
423 }
424
425 /// Returns this number rounded away from zero at a particular digit position.
426 ///
427 /// # Examples
428 ///
429 /// ```
430 /// use fixed_decimal::Decimal;
431 /// # use std::str::FromStr;
432 ///
433 /// let dec = Decimal::from_str("-1.5").unwrap();
434 /// assert_eq!("-2", dec.expanded(0).to_string());
435 /// let dec = Decimal::from_str("0.4").unwrap();
436 /// assert_eq!("1", dec.expanded(0).to_string());
437 /// let dec = Decimal::from_str("0.5").unwrap();
438 /// assert_eq!("1", dec.expanded(0).to_string());
439 /// let dec = Decimal::from_str("0.6").unwrap();
440 /// assert_eq!("1", dec.expanded(0).to_string());
441 /// let dec = Decimal::from_str("1.5").unwrap();
442 /// assert_eq!("2", dec.expanded(0).to_string());
443 /// ```
444 pub fn expanded(mut self, position: i16) -> Self {
445 self.expand(position);
446 self
447 }
448
449 /// Rounds this number towards negative infinity at a particular digit position.
450 ///
451 /// # Examples
452 ///
453 /// ```
454 /// use fixed_decimal::Decimal;
455 /// # use std::str::FromStr;
456 ///
457 /// let mut dec = Decimal::from_str("-1.5").unwrap();
458 /// dec.floor(0);
459 /// assert_eq!("-2", dec.to_string());
460 /// let mut dec = Decimal::from_str("0.4").unwrap();
461 /// dec.floor(0);
462 /// assert_eq!("0", dec.to_string());
463 /// let mut dec = Decimal::from_str("0.5").unwrap();
464 /// dec.floor(0);
465 /// assert_eq!("0", dec.to_string());
466 /// let mut dec = Decimal::from_str("0.6").unwrap();
467 /// dec.floor(0);
468 /// assert_eq!("0", dec.to_string());
469 /// let mut dec = Decimal::from_str("1.5").unwrap();
470 /// dec.floor(0);
471 /// assert_eq!("1", dec.to_string());
472 /// ```
473 #[inline(never)]
474 pub fn floor(&mut self, position: i16) {
475 self.floor_to_increment_internal(position, NoIncrement);
476 }
477
478 /// Returns this number rounded towards negative infinity at a particular digit position.
479 ///
480 /// # Examples
481 ///
482 /// ```
483 /// use fixed_decimal::Decimal;
484 /// # use std::str::FromStr;
485 ///
486 /// let dec = Decimal::from_str("-1.5").unwrap();
487 /// assert_eq!("-2", dec.floored(0).to_string());
488 /// let dec = Decimal::from_str("0.4").unwrap();
489 /// assert_eq!("0", dec.floored(0).to_string());
490 /// let dec = Decimal::from_str("0.5").unwrap();
491 /// assert_eq!("0", dec.floored(0).to_string());
492 /// let dec = Decimal::from_str("0.6").unwrap();
493 /// assert_eq!("0", dec.floored(0).to_string());
494 /// let dec = Decimal::from_str("1.5").unwrap();
495 /// assert_eq!("1", dec.floored(0).to_string());
496 /// ```
497 pub fn floored(mut self, position: i16) -> Self {
498 self.floor(position);
499 self
500 }
501
502 /// Rounds this number towards zero at a particular digit position.
503 ///
504 /// Also see [`UnsignedDecimal::pad_end()`].
505 ///
506 /// # Examples
507 ///
508 /// ```
509 /// use fixed_decimal::Decimal;
510 /// # use std::str::FromStr;
511 ///
512 /// let mut dec = Decimal::from_str("-1.5").unwrap();
513 /// dec.trunc(0);
514 /// assert_eq!("-1", dec.to_string());
515 /// let mut dec = Decimal::from_str("0.4").unwrap();
516 /// dec.trunc(0);
517 /// assert_eq!("0", dec.to_string());
518 /// let mut dec = Decimal::from_str("0.5").unwrap();
519 /// dec.trunc(0);
520 /// assert_eq!("0", dec.to_string());
521 /// let mut dec = Decimal::from_str("0.6").unwrap();
522 /// dec.trunc(0);
523 /// assert_eq!("0", dec.to_string());
524 /// let mut dec = Decimal::from_str("1.5").unwrap();
525 /// dec.trunc(0);
526 /// assert_eq!("1", dec.to_string());
527 /// ```
528 #[inline(never)]
529 pub fn trunc(&mut self, position: i16) {
530 self.trunc_to_increment_internal(position, NoIncrement)
531 }
532
533 /// Returns this number rounded towards zero at a particular digit position.
534 ///
535 /// Also see [`UnsignedDecimal::padded_end()`].
536 ///
537 /// # Examples
538 ///
539 /// ```
540 /// use fixed_decimal::Decimal;
541 /// # use std::str::FromStr;
542 ///
543 /// let dec = Decimal::from_str("-1.5").unwrap();
544 /// assert_eq!("-1", dec.trunced(0).to_string());
545 /// let dec = Decimal::from_str("0.4").unwrap();
546 /// assert_eq!("0", dec.trunced(0).to_string());
547 /// let dec = Decimal::from_str("0.5").unwrap();
548 /// assert_eq!("0", dec.trunced(0).to_string());
549 /// let dec = Decimal::from_str("0.6").unwrap();
550 /// assert_eq!("0", dec.trunced(0).to_string());
551 /// let dec = Decimal::from_str("1.5").unwrap();
552 /// assert_eq!("1", dec.trunced(0).to_string());
553 /// ```
554 pub fn trunced(mut self, position: i16) -> Self {
555 self.trunc(position);
556 self
557 }
558
559 /// Rounds this number at a particular digit position, using the specified rounding mode.
560 ///
561 /// # Examples
562 ///
563 /// ```
564 /// use fixed_decimal::{Decimal, SignedRoundingMode, UnsignedRoundingMode};
565 /// # use std::str::FromStr;
566 ///
567 /// let mut dec = Decimal::from_str("-3.5").unwrap();
568 /// dec.round_with_mode(0, SignedRoundingMode::Floor);
569 /// assert_eq!("-4", dec.to_string());
570 /// let mut dec = Decimal::from_str("-3.5").unwrap();
571 /// dec.round_with_mode(0, SignedRoundingMode::Ceil);
572 /// assert_eq!("-3", dec.to_string());
573 /// let mut dec = Decimal::from_str("5.455").unwrap();
574 /// dec.round_with_mode(
575 /// -2,
576 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
577 /// );
578 /// assert_eq!("5.46", dec.to_string());
579 /// let mut dec = Decimal::from_str("-7.235").unwrap();
580 /// dec.round_with_mode(
581 /// -2,
582 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
583 /// );
584 /// assert_eq!("-7.23", dec.to_string());
585 /// let mut dec = Decimal::from_str("9.75").unwrap();
586 /// dec.round_with_mode(
587 /// -1,
588 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
589 /// );
590 /// assert_eq!("9.8", dec.to_string());
591 /// ```
592 pub fn round_with_mode(&mut self, position: i16, mode: SignedRoundingMode) {
593 match mode {
594 SignedRoundingMode::Ceil => self.ceil_to_increment_internal(position, NoIncrement),
595 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand) => {
596 self.expand_to_increment_internal(position, NoIncrement)
597 }
598 SignedRoundingMode::Floor => self.floor_to_increment_internal(position, NoIncrement),
599 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc) => {
600 self.trunc_to_increment_internal(position, NoIncrement)
601 }
602 SignedRoundingMode::HalfCeil => {
603 self.half_ceil_to_increment_internal(position, NoIncrement)
604 }
605 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand) => {
606 self.half_expand_to_increment_internal(position, NoIncrement)
607 }
608 SignedRoundingMode::HalfFloor => {
609 self.half_floor_to_increment_internal(position, NoIncrement)
610 }
611 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc) => {
612 self.half_trunc_to_increment_internal(position, NoIncrement)
613 }
614 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven) => {
615 self.half_even_to_increment_internal(position, NoIncrement)
616 }
617 }
618 }
619
620 /// Returns this number rounded at a particular digit position, using the specified rounding mode.
621 ///
622 /// # Examples
623 ///
624 /// ```
625 /// use fixed_decimal::{Decimal, SignedRoundingMode, UnsignedRoundingMode};
626 /// # use std::str::FromStr;
627 ///
628 /// let mut dec = Decimal::from_str("-3.5").unwrap();
629 /// assert_eq!(
630 /// "-4",
631 /// dec.rounded_with_mode(0, SignedRoundingMode::Floor)
632 /// .to_string()
633 /// );
634 /// let mut dec = Decimal::from_str("-3.5").unwrap();
635 /// assert_eq!(
636 /// "-3",
637 /// dec.rounded_with_mode(0, SignedRoundingMode::Ceil)
638 /// .to_string()
639 /// );
640 /// let mut dec = Decimal::from_str("5.455").unwrap();
641 /// assert_eq!(
642 /// "5.46",
643 /// dec.rounded_with_mode(
644 /// -2,
645 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand)
646 /// )
647 /// .to_string()
648 /// );
649 /// let mut dec = Decimal::from_str("-7.235").unwrap();
650 /// assert_eq!(
651 /// "-7.23",
652 /// dec.rounded_with_mode(
653 /// -2,
654 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc)
655 /// )
656 /// .to_string()
657 /// );
658 /// let mut dec = Decimal::from_str("9.75").unwrap();
659 /// assert_eq!(
660 /// "9.8",
661 /// dec.rounded_with_mode(
662 /// -1,
663 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven)
664 /// )
665 /// .to_string()
666 /// );
667 /// ```
668 pub fn rounded_with_mode(mut self, position: i16, mode: SignedRoundingMode) -> Self {
669 self.round_with_mode(position, mode);
670 self
671 }
672
673 /// Rounds this number at a particular digit position and increment, using the specified rounding mode.
674 ///
675 /// # Examples
676 ///
677 /// ```
678 /// use fixed_decimal::{
679 /// Decimal, RoundingIncrement, SignedRoundingMode, UnsignedRoundingMode,
680 /// };
681 /// # use std::str::FromStr;
682 ///
683 /// let mut dec = Decimal::from_str("-3.5").unwrap();
684 /// dec.round_with_mode_and_increment(
685 /// 0,
686 /// SignedRoundingMode::Floor,
687 /// RoundingIncrement::MultiplesOf1,
688 /// );
689 /// assert_eq!("-4", dec.to_string());
690 /// let mut dec = Decimal::from_str("-3.59").unwrap();
691 /// dec.round_with_mode_and_increment(
692 /// -1,
693 /// SignedRoundingMode::Ceil,
694 /// RoundingIncrement::MultiplesOf2,
695 /// );
696 /// assert_eq!("-3.4", dec.to_string());
697 /// let mut dec = Decimal::from_str("5.455").unwrap();
698 /// dec.round_with_mode_and_increment(
699 /// -2,
700 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
701 /// RoundingIncrement::MultiplesOf5,
702 /// );
703 /// assert_eq!("5.45", dec.to_string());
704 /// let mut dec = Decimal::from_str("-7.235").unwrap();
705 /// dec.round_with_mode_and_increment(
706 /// -2,
707 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
708 /// RoundingIncrement::MultiplesOf25,
709 /// );
710 /// assert_eq!("-7.25", dec.to_string());
711 /// let mut dec = Decimal::from_str("9.75").unwrap();
712 /// dec.round_with_mode_and_increment(
713 /// -1,
714 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
715 /// RoundingIncrement::MultiplesOf5,
716 /// );
717 /// assert_eq!("10.0", dec.to_string());
718 /// ```
719 pub fn round_with_mode_and_increment(
720 &mut self,
721 position: i16,
722 mode: SignedRoundingMode,
723 increment: RoundingIncrement,
724 ) {
725 match mode {
726 SignedRoundingMode::Ceil => self.ceil_to_increment_internal(position, increment),
727 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand) => {
728 self.expand_to_increment_internal(position, increment)
729 }
730 SignedRoundingMode::Floor => self.floor_to_increment_internal(position, increment),
731 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc) => {
732 self.trunc_to_increment_internal(position, increment)
733 }
734 SignedRoundingMode::HalfCeil => {
735 self.half_ceil_to_increment_internal(position, increment)
736 }
737 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand) => {
738 self.half_expand_to_increment_internal(position, increment)
739 }
740 SignedRoundingMode::HalfFloor => {
741 self.half_floor_to_increment_internal(position, increment)
742 }
743 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc) => {
744 self.half_trunc_to_increment_internal(position, increment)
745 }
746 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven) => {
747 self.half_even_to_increment_internal(position, increment)
748 }
749 }
750 }
751
752 /// Returns this number rounded at a particular digit position and increment, using the specified rounding mode.
753 ///
754 /// # Examples
755 ///
756 /// ```
757 /// use fixed_decimal::{
758 /// Decimal, RoundingIncrement, SignedRoundingMode, UnsignedRoundingMode,
759 /// };
760 /// # use std::str::FromStr;
761 ///
762 /// let mut dec = Decimal::from_str("-3.5").unwrap();
763 /// assert_eq!(
764 /// "-4",
765 /// dec.rounded_with_mode_and_increment(
766 /// 0,
767 /// SignedRoundingMode::Floor,
768 /// RoundingIncrement::MultiplesOf1
769 /// )
770 /// .to_string()
771 /// );
772 /// let mut dec = Decimal::from_str("-3.59").unwrap();
773 /// assert_eq!(
774 /// "-3.4",
775 /// dec.rounded_with_mode_and_increment(
776 /// -1,
777 /// SignedRoundingMode::Ceil,
778 /// RoundingIncrement::MultiplesOf2
779 /// )
780 /// .to_string()
781 /// );
782 /// let mut dec = Decimal::from_str("5.455").unwrap();
783 /// assert_eq!(
784 /// "5.45",
785 /// dec.rounded_with_mode_and_increment(
786 /// -2,
787 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
788 /// RoundingIncrement::MultiplesOf5
789 /// )
790 /// .to_string()
791 /// );
792 /// let mut dec = Decimal::from_str("-7.235").unwrap();
793 /// assert_eq!(
794 /// "-7.25",
795 /// dec.rounded_with_mode_and_increment(
796 /// -2,
797 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
798 /// RoundingIncrement::MultiplesOf25
799 /// )
800 /// .to_string()
801 /// );
802 /// let mut dec = Decimal::from_str("9.75").unwrap();
803 /// assert_eq!(
804 /// "10.0",
805 /// dec.rounded_with_mode_and_increment(
806 /// -1,
807 /// SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
808 /// RoundingIncrement::MultiplesOf5
809 /// )
810 /// .to_string()
811 /// );
812 /// ```
813 pub fn rounded_with_mode_and_increment(
814 mut self,
815 position: i16,
816 mode: SignedRoundingMode,
817 increment: RoundingIncrement,
818 ) -> Self {
819 self.round_with_mode_and_increment(position, mode, increment);
820 self
821 }
822
823 fn ceil_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
824 if self.sign == Sign::Negative {
825 self.trunc_to_increment_internal(position, increment);
826 return;
827 }
828
829 self.expand_to_increment_internal(position, increment);
830 }
831
832 fn half_ceil_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
833 if self.sign == Sign::Negative {
834 self.half_trunc_to_increment_internal(position, increment);
835 return;
836 }
837
838 self.half_expand_to_increment_internal(position, increment);
839 }
840
841 fn floor_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
842 if self.sign == Sign::Negative {
843 self.expand_to_increment_internal(position, increment);
844 return;
845 }
846
847 self.trunc_to_increment_internal(position, increment);
848 }
849
850 fn half_floor_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
851 if self.sign == Sign::Negative {
852 self.half_expand_to_increment_internal(position, increment);
853 return;
854 }
855
856 self.half_trunc_to_increment_internal(position, increment);
857 }
858 }
859
860 /// Render the [`Decimal`] as a string of ASCII digits with a possible decimal point.
861 ///
862 /// # Examples
863 ///
864 /// ```
865 /// # use fixed_decimal::Decimal;
866 /// # use writeable::assert_writeable_eq;
867 /// #
868 /// assert_writeable_eq!(Decimal::from(42), "42");
869 /// ```
870 impl writeable::Writeable for Decimal {
871 fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
872 match self.sign {
873 Sign::Negative => sink.write_char('-')?,
874 Sign::Positive => sink.write_char('+')?,
875 Sign::None => (),
876 }
877 for m in self.absolute.magnitude_range().rev() {
878 if m == -1 {
879 sink.write_char('.')?;
880 }
881 let d = self.absolute.digit_at(m);
882 sink.write_char((b'0' + d) as char)?;
883 }
884 Ok(())
885 }
886
887 fn writeable_length_hint(&self) -> writeable::LengthHint {
888 self.absolute.writeable_length_hint() + (self.sign != Sign::None) as usize
889 }
890 }
891
892 writeable::impl_display_with_writeable!(Decimal);
893
894 #[test]
895 fn test_basic() {
896 #[derive(Debug)]
897 struct TestCase {
898 pub input: isize,
899 pub delta: i16,
900 pub expected: &'static str,
901 }
902 let cases = [
903 TestCase {
904 input: 51423,
905 delta: 0,
906 expected: "51423",
907 },
908 TestCase {
909 input: 51423,
910 delta: -2,
911 expected: "514.23",
912 },
913 TestCase {
914 input: 51423,
915 delta: -5,
916 expected: "0.51423",
917 },
918 TestCase {
919 input: 51423,
920 delta: -8,
921 expected: "0.00051423",
922 },
923 TestCase {
924 input: 51423,
925 delta: 3,
926 expected: "51423000",
927 },
928 TestCase {
929 input: 0,
930 delta: 0,
931 expected: "0",
932 },
933 TestCase {
934 input: 0,
935 delta: -2,
936 expected: "0.00",
937 },
938 TestCase {
939 input: 0,
940 delta: 3,
941 expected: "0000",
942 },
943 TestCase {
944 input: 500,
945 delta: 0,
946 expected: "500",
947 },
948 TestCase {
949 input: 500,
950 delta: -1,
951 expected: "50.0",
952 },
953 TestCase {
954 input: 500,
955 delta: -2,
956 expected: "5.00",
957 },
958 TestCase {
959 input: 500,
960 delta: -3,
961 expected: "0.500",
962 },
963 TestCase {
964 input: 500,
965 delta: -4,
966 expected: "0.0500",
967 },
968 TestCase {
969 input: 500,
970 delta: 3,
971 expected: "500000",
972 },
973 TestCase {
974 input: -123,
975 delta: 0,
976 expected: "-123",
977 },
978 TestCase {
979 input: -123,
980 delta: -2,
981 expected: "-1.23",
982 },
983 TestCase {
984 input: -123,
985 delta: -5,
986 expected: "-0.00123",
987 },
988 TestCase {
989 input: -123,
990 delta: 3,
991 expected: "-123000",
992 },
993 ];
994 for cas in &cases {
995 let mut dec: Decimal = cas.input.into();
996 // println!("{}", cas.input + 0.01);
997 dec.absolute.multiply_pow10(cas.delta);
998 writeable::assert_writeable_eq!(dec, cas.expected, "{:?}", cas);
999 }
1000 }
1001
1002 #[test]
1003 fn test_from_str() {
1004 #[derive(Debug)]
1005 struct TestCase {
1006 pub input_str: &'static str,
1007 /// The output str, `None` for roundtrip
1008 pub output_str: Option<&'static str>,
1009 /// [upper magnitude, upper nonzero magnitude, lower nonzero magnitude, lower magnitude]
1010 pub magnitudes: [i16; 4],
1011 }
1012 let cases = [
1013 TestCase {
1014 input_str: "-00123400",
1015 output_str: None,
1016 magnitudes: [7, 5, 2, 0],
1017 },
1018 TestCase {
1019 input_str: "+00123400",
1020 output_str: None,
1021 magnitudes: [7, 5, 2, 0],
1022 },
1023 TestCase {
1024 input_str: "0.0123400",
1025 output_str: None,
1026 magnitudes: [0, -2, -5, -7],
1027 },
1028 TestCase {
1029 input_str: "-00.123400",
1030 output_str: None,
1031 magnitudes: [1, -1, -4, -6],
1032 },
1033 TestCase {
1034 input_str: "0012.3400",
1035 output_str: None,
1036 magnitudes: [3, 1, -2, -4],
1037 },
1038 TestCase {
1039 input_str: "-0012340.0",
1040 output_str: None,
1041 magnitudes: [6, 4, 1, -1],
1042 },
1043 TestCase {
1044 input_str: "1234",
1045 output_str: None,
1046 magnitudes: [3, 3, 0, 0],
1047 },
1048 TestCase {
1049 input_str: "0.000000001",
1050 output_str: None,
1051 magnitudes: [0, -9, -9, -9],
1052 },
1053 TestCase {
1054 input_str: "0.0000000010",
1055 output_str: None,
1056 magnitudes: [0, -9, -9, -10],
1057 },
1058 TestCase {
1059 input_str: "1000000",
1060 output_str: None,
1061 magnitudes: [6, 6, 6, 0],
1062 },
1063 TestCase {
1064 input_str: "10000001",
1065 output_str: None,
1066 magnitudes: [7, 7, 0, 0],
1067 },
1068 TestCase {
1069 input_str: "123",
1070 output_str: None,
1071 magnitudes: [2, 2, 0, 0],
1072 },
1073 TestCase {
1074 input_str: "922337203685477580898230948203840239384.9823094820384023938423424",
1075 output_str: None,
1076 magnitudes: [38, 38, -25, -25],
1077 },
1078 TestCase {
1079 input_str: "009223372000.003685477580898230948203840239384000",
1080 output_str: None,
1081 magnitudes: [11, 9, -33, -36],
1082 },
1083 TestCase {
1084 input_str: "-009223372000.003685477580898230948203840239384000",
1085 output_str: None,
1086 magnitudes: [11, 9, -33, -36],
1087 },
1088 TestCase {
1089 input_str: "0",
1090 output_str: None,
1091 magnitudes: [0, 0, 0, 0],
1092 },
1093 TestCase {
1094 input_str: "-0",
1095 output_str: None,
1096 magnitudes: [0, 0, 0, 0],
1097 },
1098 TestCase {
1099 input_str: "+0",
1100 output_str: None,
1101 magnitudes: [0, 0, 0, 0],
1102 },
1103 TestCase {
1104 input_str: "000",
1105 output_str: None,
1106 magnitudes: [2, 0, 0, 0],
1107 },
1108 TestCase {
1109 input_str: "-00.0",
1110 output_str: None,
1111 magnitudes: [1, 0, 0, -1],
1112 },
1113 // no leading 0 parsing
1114 TestCase {
1115 input_str: ".0123400",
1116 output_str: Some("0.0123400"),
1117 magnitudes: [0, -2, -5, -7],
1118 },
1119 TestCase {
1120 input_str: ".000000001",
1121 output_str: Some("0.000000001"),
1122 magnitudes: [0, -9, -9, -9],
1123 },
1124 TestCase {
1125 input_str: "-.123400",
1126 output_str: Some("-0.123400"),
1127 magnitudes: [0, -1, -4, -6],
1128 },
1129 ];
1130 for cas in &cases {
1131 let fd = Decimal::from_str(cas.input_str).unwrap();
1132 assert_eq!(
1133 fd.absolute.magnitude_range(),
1134 cas.magnitudes[3]..=cas.magnitudes[0],
1135 "{cas:?}"
1136 );
1137 assert_eq!(
1138 fd.absolute.nonzero_magnitude_start(),
1139 cas.magnitudes[1],
1140 "{cas:?}"
1141 );
1142 assert_eq!(
1143 fd.absolute.nonzero_magnitude_end(),
1144 cas.magnitudes[2],
1145 "{cas:?}"
1146 );
1147 let input_str_roundtrip = fd.to_string();
1148 let output_str = cas.output_str.unwrap_or(cas.input_str);
1149 assert_eq!(output_str, input_str_roundtrip, "{cas:?}");
1150 }
1151 }
1152
1153 #[test]
1154 fn test_from_str_scientific() {
1155 #[derive(Debug)]
1156 struct TestCase {
1157 pub input_str: &'static str,
1158 pub output: &'static str,
1159 }
1160 let cases = [
1161 TestCase {
1162 input_str: "-5.4e10",
1163 output: "-54000000000",
1164 },
1165 TestCase {
1166 input_str: "5.4e-2",
1167 output: "0.054",
1168 },
1169 TestCase {
1170 input_str: "54.1e-2",
1171 output: "0.541",
1172 },
1173 TestCase {
1174 input_str: "-541e-2",
1175 output: "-5.41",
1176 },
1177 TestCase {
1178 input_str: "0.009E10",
1179 output: "90000000",
1180 },
1181 TestCase {
1182 input_str: "-9000E-10",
1183 output: "-0.0000009",
1184 },
1185 ];
1186 for cas in &cases {
1187 let input_str_roundtrip = Decimal::from_str(cas.input_str).unwrap().to_string();
1188 assert_eq!(cas.output, input_str_roundtrip);
1189 }
1190 }
1191
1192 #[test]
1193 fn test_isize_limits() {
1194 for num in &[isize::MAX, isize::MIN] {
1195 let dec: Decimal = (*num).into();
1196 let dec_str = dec.to_string();
1197 assert_eq!(num.to_string(), dec_str);
1198 assert_eq!(dec, Decimal::from_str(&dec_str).unwrap());
1199 writeable::assert_writeable_eq!(dec, dec_str);
1200 }
1201 }
1202
1203 #[test]
1204 fn test_ui128_limits() {
1205 for num in &[i128::MAX, i128::MIN] {
1206 let dec: Decimal = (*num).into();
1207 let dec_str = dec.to_string();
1208 assert_eq!(num.to_string(), dec_str);
1209 assert_eq!(dec, Decimal::from_str(&dec_str).unwrap());
1210 writeable::assert_writeable_eq!(dec, dec_str);
1211 }
1212 for num in &[u128::MAX, u128::MIN] {
1213 let dec: Decimal = (*num).into();
1214 let dec_str = dec.to_string();
1215 assert_eq!(num.to_string(), dec_str);
1216 assert_eq!(dec, Decimal::from_str(&dec_str).unwrap());
1217 writeable::assert_writeable_eq!(dec, dec_str);
1218 }
1219 }
1220
1221 #[test]
1222 fn test_zero_str_bounds() {
1223 #[derive(Debug)]
1224 struct TestCase {
1225 pub zeros_before_dot: usize,
1226 pub zeros_after_dot: usize,
1227 pub expected_err: Option<ParseError>,
1228 }
1229 let cases = [
1230 TestCase {
1231 zeros_before_dot: i16::MAX as usize + 1,
1232 zeros_after_dot: 0,
1233 expected_err: None,
1234 },
1235 TestCase {
1236 zeros_before_dot: i16::MAX as usize,
1237 zeros_after_dot: 0,
1238 expected_err: None,
1239 },
1240 TestCase {
1241 zeros_before_dot: i16::MAX as usize + 2,
1242 zeros_after_dot: 0,
1243 expected_err: Some(ParseError::Limit),
1244 },
1245 TestCase {
1246 zeros_before_dot: 0,
1247 zeros_after_dot: i16::MAX as usize + 2,
1248 expected_err: Some(ParseError::Limit),
1249 },
1250 TestCase {
1251 zeros_before_dot: i16::MAX as usize + 1,
1252 zeros_after_dot: i16::MAX as usize + 1,
1253 expected_err: None,
1254 },
1255 TestCase {
1256 zeros_before_dot: i16::MAX as usize + 2,
1257 zeros_after_dot: i16::MAX as usize + 1,
1258 expected_err: Some(ParseError::Limit),
1259 },
1260 TestCase {
1261 zeros_before_dot: i16::MAX as usize + 1,
1262 zeros_after_dot: i16::MAX as usize + 2,
1263 expected_err: Some(ParseError::Limit),
1264 },
1265 TestCase {
1266 zeros_before_dot: i16::MAX as usize,
1267 zeros_after_dot: i16::MAX as usize + 2,
1268 expected_err: Some(ParseError::Limit),
1269 },
1270 TestCase {
1271 zeros_before_dot: i16::MAX as usize,
1272 zeros_after_dot: i16::MAX as usize,
1273 expected_err: None,
1274 },
1275 TestCase {
1276 zeros_before_dot: i16::MAX as usize + 1,
1277 zeros_after_dot: i16::MAX as usize,
1278 expected_err: None,
1279 },
1280 ];
1281 for cas in &cases {
1282 let mut input_str = format!("{:0fill$}", 0, fill = cas.zeros_before_dot);
1283 if cas.zeros_after_dot > 0 {
1284 input_str.push('.');
1285 input_str.push_str(&format!("{:0fill$}", 0, fill = cas.zeros_after_dot));
1286 }
1287 match Decimal::from_str(&input_str) {
1288 Ok(dec) => {
1289 assert_eq!(cas.expected_err, None, "{cas:?}");
1290 assert_eq!(input_str, dec.to_string(), "{cas:?}");
1291 }
1292 Err(err) => {
1293 assert_eq!(cas.expected_err, Some(err), "{cas:?}");
1294 }
1295 }
1296 }
1297 }
1298
1299 #[test]
1300 fn test_syntax_error() {
1301 #[derive(Debug)]
1302 struct TestCase {
1303 pub input_str: &'static str,
1304 pub expected_err: Option<ParseError>,
1305 }
1306 let cases = [
1307 TestCase {
1308 input_str: "-12a34",
1309 expected_err: Some(ParseError::Syntax),
1310 },
1311 TestCase {
1312 input_str: "0.0123√400",
1313 expected_err: Some(ParseError::Syntax),
1314 },
1315 TestCase {
1316 input_str: "0.012.3400",
1317 expected_err: Some(ParseError::Syntax),
1318 },
1319 TestCase {
1320 input_str: "-0-0123400",
1321 expected_err: Some(ParseError::Syntax),
1322 },
1323 TestCase {
1324 input_str: "0-0123400",
1325 expected_err: Some(ParseError::Syntax),
1326 },
1327 TestCase {
1328 input_str: "-0.00123400",
1329 expected_err: None,
1330 },
1331 TestCase {
1332 input_str: "00123400.",
1333 expected_err: Some(ParseError::Syntax),
1334 },
1335 TestCase {
1336 input_str: "00123400.0",
1337 expected_err: None,
1338 },
1339 TestCase {
1340 input_str: "123_456",
1341 expected_err: Some(ParseError::Syntax),
1342 },
1343 TestCase {
1344 input_str: "",
1345 expected_err: Some(ParseError::Syntax),
1346 },
1347 TestCase {
1348 input_str: "-",
1349 expected_err: Some(ParseError::Syntax),
1350 },
1351 TestCase {
1352 input_str: "+",
1353 expected_err: Some(ParseError::Syntax),
1354 },
1355 TestCase {
1356 input_str: "-1",
1357 expected_err: None,
1358 },
1359 ];
1360 for cas in &cases {
1361 match Decimal::from_str(cas.input_str) {
1362 Ok(dec) => {
1363 assert_eq!(cas.expected_err, None, "{cas:?}");
1364 assert_eq!(cas.input_str, dec.to_string(), "{cas:?}");
1365 }
1366 Err(err) => {
1367 assert_eq!(cas.expected_err, Some(err), "{cas:?}");
1368 }
1369 }
1370 }
1371 }
1372
1373 #[test]
1374 fn test_pad() {
1375 let mut dec = Decimal::from_str("-0.42").unwrap();
1376 assert_eq!("-0.42", dec.to_string());
1377
1378 dec.absolute.pad_start(1);
1379 assert_eq!("-0.42", dec.to_string());
1380
1381 dec.absolute.pad_start(4);
1382 assert_eq!("-0000.42", dec.to_string());
1383
1384 dec.absolute.pad_start(2);
1385 assert_eq!("-00.42", dec.to_string());
1386 }
1387
1388 #[test]
1389 fn test_sign_display() {
1390 use SignDisplay::*;
1391 let positive_nonzero = Decimal::from(163u32);
1392 let negative_nonzero = Decimal::from(-163);
1393 let positive_zero = Decimal::from(0u32);
1394 let negative_zero = Decimal::from(0u32).with_sign(Sign::Negative);
1395 assert_eq!(
1396 "163",
1397 positive_nonzero.clone().with_sign_display(Auto).to_string()
1398 );
1399 assert_eq!(
1400 "-163",
1401 negative_nonzero.clone().with_sign_display(Auto).to_string()
1402 );
1403 assert_eq!(
1404 "0",
1405 positive_zero.clone().with_sign_display(Auto).to_string()
1406 );
1407 assert_eq!(
1408 "-0",
1409 negative_zero.clone().with_sign_display(Auto).to_string()
1410 );
1411 assert_eq!(
1412 "+163",
1413 positive_nonzero
1414 .clone()
1415 .with_sign_display(Always)
1416 .to_string()
1417 );
1418 assert_eq!(
1419 "-163",
1420 negative_nonzero
1421 .clone()
1422 .with_sign_display(Always)
1423 .to_string()
1424 );
1425 assert_eq!(
1426 "+0",
1427 positive_zero.clone().with_sign_display(Always).to_string()
1428 );
1429 assert_eq!(
1430 "-0",
1431 negative_zero.clone().with_sign_display(Always).to_string()
1432 );
1433 assert_eq!(
1434 "163",
1435 positive_nonzero
1436 .clone()
1437 .with_sign_display(Never)
1438 .to_string()
1439 );
1440 assert_eq!(
1441 "163",
1442 negative_nonzero
1443 .clone()
1444 .with_sign_display(Never)
1445 .to_string()
1446 );
1447 assert_eq!(
1448 "0",
1449 positive_zero.clone().with_sign_display(Never).to_string()
1450 );
1451 assert_eq!(
1452 "0",
1453 negative_zero.clone().with_sign_display(Never).to_string()
1454 );
1455 assert_eq!(
1456 "+163",
1457 positive_nonzero
1458 .clone()
1459 .with_sign_display(ExceptZero)
1460 .to_string()
1461 );
1462 assert_eq!(
1463 "-163",
1464 negative_nonzero
1465 .clone()
1466 .with_sign_display(ExceptZero)
1467 .to_string()
1468 );
1469 assert_eq!(
1470 "0",
1471 positive_zero
1472 .clone()
1473 .with_sign_display(ExceptZero)
1474 .to_string()
1475 );
1476 assert_eq!(
1477 "0",
1478 negative_zero
1479 .clone()
1480 .with_sign_display(ExceptZero)
1481 .to_string()
1482 );
1483 assert_eq!(
1484 "163",
1485 positive_nonzero.with_sign_display(Negative).to_string()
1486 );
1487 assert_eq!(
1488 "-163",
1489 negative_nonzero.with_sign_display(Negative).to_string()
1490 );
1491 assert_eq!("0", positive_zero.with_sign_display(Negative).to_string());
1492 assert_eq!("0", negative_zero.with_sign_display(Negative).to_string());
1493 }
1494
1495 #[test]
1496 fn test_set_max_position() {
1497 let mut dec = Decimal::from(1000u32);
1498 assert_eq!("1000", dec.to_string());
1499
1500 dec.absolute.set_max_position(2);
1501 assert_eq!("00", dec.to_string());
1502
1503 dec.absolute.set_max_position(0);
1504 assert_eq!("0", dec.to_string());
1505
1506 dec.absolute.set_max_position(3);
1507 assert_eq!("000", dec.to_string());
1508
1509 let mut dec = Decimal::from_str("0.456").unwrap();
1510 assert_eq!("0.456", dec.to_string());
1511
1512 dec.absolute.set_max_position(0);
1513 assert_eq!("0.456", dec.to_string());
1514
1515 dec.absolute.set_max_position(-1);
1516 assert_eq!("0.056", dec.to_string());
1517
1518 dec.absolute.set_max_position(-2);
1519 assert_eq!("0.006", dec.to_string());
1520
1521 dec.absolute.set_max_position(-3);
1522 assert_eq!("0.000", dec.to_string());
1523
1524 dec.absolute.set_max_position(-4);
1525 assert_eq!("0.0000", dec.to_string());
1526
1527 let mut dec = Decimal::from_str("100.01").unwrap();
1528 dec.absolute.set_max_position(1);
1529 assert_eq!("0.01", dec.to_string());
1530 }
1531
1532 #[test]
1533 fn test_pad_start_bounds() {
1534 let mut dec = Decimal::from_str("299792.458").unwrap();
1535 let max_integer_digits = i16::MAX as usize + 1;
1536
1537 dec.absolute.pad_start(i16::MAX - 1);
1538 assert_eq!(
1539 max_integer_digits - 2,
1540 dec.to_string().split_once('.').unwrap().0.len()
1541 );
1542
1543 dec.absolute.pad_start(i16::MAX);
1544 assert_eq!(
1545 max_integer_digits - 1,
1546 dec.to_string().split_once('.').unwrap().0.len()
1547 );
1548 }
1549
1550 #[test]
1551 fn test_pad_end_bounds() {
1552 let mut dec = Decimal::from_str("299792.458").unwrap();
1553 let max_fractional_digits = -(i16::MIN as isize) as usize;
1554
1555 dec.absolute.pad_end(i16::MIN + 1);
1556 assert_eq!(
1557 max_fractional_digits - 1,
1558 dec.to_string().split_once('.').unwrap().1.len()
1559 );
1560
1561 dec.absolute.pad_end(i16::MIN);
1562 assert_eq!(
1563 max_fractional_digits,
1564 dec.to_string().split_once('.').unwrap().1.len()
1565 );
1566 }
1567
1568 #[test]
1569 fn test_rounding() {
1570 pub(crate) use std::str::FromStr;
1571
1572 // Test Ceil
1573 let mut dec = Decimal::from_str("3.234").unwrap();
1574 dec.ceil(0);
1575 assert_eq!("4", dec.to_string());
1576
1577 let mut dec = Decimal::from_str("2.222").unwrap();
1578 dec.ceil(-1);
1579 assert_eq!("2.3", dec.to_string());
1580
1581 let mut dec = Decimal::from_str("22.222").unwrap();
1582 dec.ceil(-2);
1583 assert_eq!("22.23", dec.to_string());
1584
1585 let mut dec = Decimal::from_str("99.999").unwrap();
1586 dec.ceil(-2);
1587 assert_eq!("100.00", dec.to_string());
1588
1589 let mut dec = Decimal::from_str("99.999").unwrap();
1590 dec.ceil(-5);
1591 assert_eq!("99.99900", dec.to_string());
1592
1593 let mut dec = Decimal::from_str("-99.999").unwrap();
1594 dec.ceil(-5);
1595 assert_eq!("-99.99900", dec.to_string());
1596
1597 let mut dec = Decimal::from_str("-99.999").unwrap();
1598 dec.ceil(-2);
1599 assert_eq!("-99.99", dec.to_string());
1600
1601 let mut dec = Decimal::from_str("99.999").unwrap();
1602 dec.ceil(4);
1603 assert_eq!("10000", dec.to_string());
1604
1605 let mut dec = Decimal::from_str("-99.999").unwrap();
1606 dec.ceil(4);
1607 assert_eq!("-0000", dec.to_string());
1608
1609 let mut dec = Decimal::from_str("0.009").unwrap();
1610 dec.ceil(-1);
1611 assert_eq!("0.1", dec.to_string());
1612
1613 let mut dec = Decimal::from_str("-0.009").unwrap();
1614 dec.ceil(-1);
1615 assert_eq!("-0.0", dec.to_string());
1616
1617 // Test Half Ceil
1618 let mut dec = Decimal::from_str("3.234").unwrap();
1619 dec.round_with_mode(0, SignedRoundingMode::HalfCeil);
1620 assert_eq!("3", dec.to_string());
1621
1622 let mut dec = Decimal::from_str("3.534").unwrap();
1623 dec.round_with_mode(0, SignedRoundingMode::HalfCeil);
1624 assert_eq!("4", dec.to_string());
1625
1626 let mut dec = Decimal::from_str("3.934").unwrap();
1627 dec.round_with_mode(0, SignedRoundingMode::HalfCeil);
1628 assert_eq!("4", dec.to_string());
1629
1630 let mut dec = Decimal::from_str("2.222").unwrap();
1631 dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
1632 assert_eq!("2.2", dec.to_string());
1633
1634 let mut dec = Decimal::from_str("2.44").unwrap();
1635 dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
1636 assert_eq!("2.4", dec.to_string());
1637
1638 let mut dec = Decimal::from_str("2.45").unwrap();
1639 dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
1640 assert_eq!("2.5", dec.to_string());
1641
1642 let mut dec = Decimal::from_str("-2.44").unwrap();
1643 dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
1644 assert_eq!("-2.4", dec.to_string());
1645
1646 let mut dec = Decimal::from_str("-2.45").unwrap();
1647 dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
1648 assert_eq!("-2.4", dec.to_string());
1649
1650 let mut dec = Decimal::from_str("22.222").unwrap();
1651 dec.round_with_mode(-2, SignedRoundingMode::HalfCeil);
1652 assert_eq!("22.22", dec.to_string());
1653
1654 let mut dec = Decimal::from_str("99.999").unwrap();
1655 dec.round_with_mode(-2, SignedRoundingMode::HalfCeil);
1656 assert_eq!("100.00", dec.to_string());
1657
1658 let mut dec = Decimal::from_str("99.999").unwrap();
1659 dec.round_with_mode(-5, SignedRoundingMode::HalfCeil);
1660 assert_eq!("99.99900", dec.to_string());
1661
1662 let mut dec = Decimal::from_str("-99.999").unwrap();
1663 dec.round_with_mode(-5, SignedRoundingMode::HalfCeil);
1664 assert_eq!("-99.99900", dec.to_string());
1665
1666 let mut dec = Decimal::from_str("-99.999").unwrap();
1667 dec.round_with_mode(-2, SignedRoundingMode::HalfCeil);
1668 assert_eq!("-100.00", dec.to_string());
1669
1670 let mut dec = Decimal::from_str("99.999").unwrap();
1671 dec.round_with_mode(4, SignedRoundingMode::HalfCeil);
1672 assert_eq!("0000", dec.to_string());
1673
1674 let mut dec = Decimal::from_str("-99.999").unwrap();
1675 dec.round_with_mode(4, SignedRoundingMode::HalfCeil);
1676 assert_eq!("-0000", dec.to_string());
1677
1678 let mut dec = Decimal::from_str("0.009").unwrap();
1679 dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
1680 assert_eq!("0.0", dec.to_string());
1681
1682 let mut dec = Decimal::from_str("-0.009").unwrap();
1683 dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
1684 assert_eq!("-0.0", dec.to_string());
1685
1686 // Test Floor
1687 let mut dec = Decimal::from_str("3.234").unwrap();
1688 dec.floor(0);
1689 assert_eq!("3", dec.to_string());
1690
1691 let mut dec = Decimal::from_str("2.222").unwrap();
1692 dec.floor(-1);
1693 assert_eq!("2.2", dec.to_string());
1694
1695 let mut dec = Decimal::from_str("99.999").unwrap();
1696 dec.floor(-2);
1697 assert_eq!("99.99", dec.to_string());
1698
1699 let mut dec = Decimal::from_str("99.999").unwrap();
1700 dec.floor(-10);
1701 assert_eq!("99.9990000000", dec.to_string());
1702
1703 let mut dec = Decimal::from_str("-99.999").unwrap();
1704 dec.floor(-10);
1705 assert_eq!("-99.9990000000", dec.to_string());
1706
1707 let mut dec = Decimal::from_str("99.999").unwrap();
1708 dec.floor(10);
1709 assert_eq!("0000000000", dec.to_string());
1710
1711 let mut dec = Decimal::from_str("-99.999").unwrap();
1712 dec.floor(10);
1713 assert_eq!("-10000000000", dec.to_string());
1714
1715 // Test Half Floor
1716 let mut dec = Decimal::from_str("3.234").unwrap();
1717 dec.round_with_mode(0, SignedRoundingMode::HalfFloor);
1718 assert_eq!("3", dec.to_string());
1719
1720 let mut dec = Decimal::from_str("3.534").unwrap();
1721 dec.round_with_mode(0, SignedRoundingMode::HalfFloor);
1722 assert_eq!("4", dec.to_string());
1723
1724 let mut dec = Decimal::from_str("3.934").unwrap();
1725 dec.round_with_mode(0, SignedRoundingMode::HalfFloor);
1726 assert_eq!("4", dec.to_string());
1727
1728 let mut dec = Decimal::from_str("2.222").unwrap();
1729 dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
1730 assert_eq!("2.2", dec.to_string());
1731
1732 let mut dec = Decimal::from_str("2.44").unwrap();
1733 dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
1734 assert_eq!("2.4", dec.to_string());
1735
1736 let mut dec = Decimal::from_str("2.45").unwrap();
1737 dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
1738 assert_eq!("2.4", dec.to_string());
1739
1740 let mut dec = Decimal::from_str("-2.44").unwrap();
1741 dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
1742 assert_eq!("-2.4", dec.to_string());
1743
1744 let mut dec = Decimal::from_str("-2.45").unwrap();
1745 dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
1746 assert_eq!("-2.5", dec.to_string());
1747
1748 let mut dec = Decimal::from_str("22.222").unwrap();
1749 dec.round_with_mode(-2, SignedRoundingMode::HalfFloor);
1750 assert_eq!("22.22", dec.to_string());
1751
1752 let mut dec = Decimal::from_str("99.999").unwrap();
1753 dec.round_with_mode(-2, SignedRoundingMode::HalfFloor);
1754 assert_eq!("100.00", dec.to_string());
1755
1756 let mut dec = Decimal::from_str("99.999").unwrap();
1757 dec.round_with_mode(-5, SignedRoundingMode::HalfFloor);
1758 assert_eq!("99.99900", dec.to_string());
1759
1760 let mut dec = Decimal::from_str("-99.999").unwrap();
1761 dec.round_with_mode(-5, SignedRoundingMode::HalfFloor);
1762 assert_eq!("-99.99900", dec.to_string());
1763
1764 let mut dec = Decimal::from_str("-99.999").unwrap();
1765 dec.round_with_mode(-2, SignedRoundingMode::HalfFloor);
1766 assert_eq!("-100.00", dec.to_string());
1767
1768 let mut dec = Decimal::from_str("99.999").unwrap();
1769 dec.round_with_mode(4, SignedRoundingMode::HalfFloor);
1770 assert_eq!("0000", dec.to_string());
1771
1772 let mut dec = Decimal::from_str("-99.999").unwrap();
1773 dec.round_with_mode(4, SignedRoundingMode::HalfFloor);
1774 assert_eq!("-0000", dec.to_string());
1775
1776 let mut dec = Decimal::from_str("0.009").unwrap();
1777 dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
1778 assert_eq!("0.0", dec.to_string());
1779
1780 let mut dec = Decimal::from_str("-0.009").unwrap();
1781 dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
1782 assert_eq!("-0.0", dec.to_string());
1783
1784 // Test Truncate Right
1785 let mut dec = Decimal::from(4235970u32);
1786 dec.multiply_pow10(-3);
1787 assert_eq!("4235.970", dec.to_string());
1788
1789 dec.trunc(-5);
1790 assert_eq!("4235.97000", dec.to_string());
1791
1792 dec.trunc(-1);
1793 assert_eq!("4235.9", dec.to_string());
1794
1795 dec.trunc(0);
1796 assert_eq!("4235", dec.to_string());
1797
1798 dec.trunc(1);
1799 assert_eq!("4230", dec.to_string());
1800
1801 dec.trunc(5);
1802 assert_eq!("00000", dec.to_string());
1803
1804 dec.trunc(2);
1805 assert_eq!("00000", dec.to_string());
1806
1807 let mut dec = Decimal::from_str("-99.999").unwrap();
1808 dec.trunc(-2);
1809 assert_eq!("-99.99", dec.to_string());
1810
1811 let mut dec = Decimal::from_str("1234.56").unwrap();
1812 dec.trunc(-1);
1813 assert_eq!("1234.5", dec.to_string());
1814
1815 let mut dec = Decimal::from_str("0.009").unwrap();
1816 dec.trunc(-1);
1817 assert_eq!("0.0", dec.to_string());
1818
1819 // Test trunced
1820 let mut dec = Decimal::from(4235970u32);
1821 dec.multiply_pow10(-3);
1822 assert_eq!("4235.970", dec.to_string());
1823
1824 assert_eq!("4235.97000", dec.clone().trunced(-5).to_string());
1825
1826 assert_eq!("4230", dec.clone().trunced(1).to_string());
1827
1828 assert_eq!("4200", dec.clone().trunced(2).to_string());
1829
1830 assert_eq!("00000", dec.trunced(5).to_string());
1831
1832 //Test expand
1833 let mut dec = Decimal::from_str("3.234").unwrap();
1834 dec.expand(0);
1835 assert_eq!("4", dec.to_string());
1836
1837 let mut dec = Decimal::from_str("2.222").unwrap();
1838 dec.expand(-1);
1839 assert_eq!("2.3", dec.to_string());
1840
1841 let mut dec = Decimal::from_str("22.222").unwrap();
1842 dec.expand(-2);
1843 assert_eq!("22.23", dec.to_string());
1844
1845 let mut dec = Decimal::from_str("99.999").unwrap();
1846 dec.expand(-2);
1847 assert_eq!("100.00", dec.to_string());
1848
1849 let mut dec = Decimal::from_str("99.999").unwrap();
1850 dec.expand(-5);
1851 assert_eq!("99.99900", dec.to_string());
1852
1853 let mut dec = Decimal::from_str("-99.999").unwrap();
1854 dec.expand(-5);
1855 assert_eq!("-99.99900", dec.to_string());
1856
1857 let mut dec = Decimal::from_str("-99.999").unwrap();
1858 dec.expand(-2);
1859 assert_eq!("-100.00", dec.to_string());
1860
1861 let mut dec = Decimal::from_str("99.999").unwrap();
1862 dec.expand(4);
1863 assert_eq!("10000", dec.to_string());
1864
1865 let mut dec = Decimal::from_str("-99.999").unwrap();
1866 dec.expand(4);
1867 assert_eq!("-10000", dec.to_string());
1868
1869 let mut dec = Decimal::from_str("0.009").unwrap();
1870 dec.expand(-1);
1871 assert_eq!("0.1", dec.to_string());
1872
1873 let mut dec = Decimal::from_str("-0.009").unwrap();
1874 dec.expand(-1);
1875 assert_eq!("-0.1", dec.to_string());
1876
1877 let mut dec = Decimal::from_str("3.954").unwrap();
1878 dec.expand(0);
1879 assert_eq!("4", dec.to_string());
1880
1881 // Test half_expand
1882 let mut dec = Decimal::from_str("3.234").unwrap();
1883 dec.round_with_mode(
1884 0,
1885 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1886 );
1887 assert_eq!("3", dec.to_string());
1888
1889 let mut dec = Decimal::from_str("3.534").unwrap();
1890 dec.round_with_mode(
1891 0,
1892 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1893 );
1894 assert_eq!("4", dec.to_string());
1895
1896 let mut dec = Decimal::from_str("3.934").unwrap();
1897 dec.round_with_mode(
1898 0,
1899 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1900 );
1901 assert_eq!("4", dec.to_string());
1902
1903 let mut dec = Decimal::from_str("2.222").unwrap();
1904 dec.round_with_mode(
1905 -1,
1906 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1907 );
1908 assert_eq!("2.2", dec.to_string());
1909
1910 let mut dec = Decimal::from_str("2.44").unwrap();
1911 dec.round_with_mode(
1912 -1,
1913 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1914 );
1915 assert_eq!("2.4", dec.to_string());
1916
1917 let mut dec = Decimal::from_str("2.45").unwrap();
1918 dec.round_with_mode(
1919 -1,
1920 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1921 );
1922 assert_eq!("2.5", dec.to_string());
1923
1924 let mut dec = Decimal::from_str("-2.44").unwrap();
1925 dec.round_with_mode(
1926 -1,
1927 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1928 );
1929 assert_eq!("-2.4", dec.to_string());
1930
1931 let mut dec = Decimal::from_str("-2.45").unwrap();
1932 dec.round_with_mode(
1933 -1,
1934 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1935 );
1936 assert_eq!("-2.5", dec.to_string());
1937
1938 let mut dec = Decimal::from_str("22.222").unwrap();
1939 dec.round_with_mode(
1940 -2,
1941 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1942 );
1943 assert_eq!("22.22", dec.to_string());
1944
1945 let mut dec = Decimal::from_str("99.999").unwrap();
1946 dec.round_with_mode(
1947 -2,
1948 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1949 );
1950 assert_eq!("100.00", dec.to_string());
1951
1952 let mut dec = Decimal::from_str("99.999").unwrap();
1953 dec.round_with_mode(
1954 -5,
1955 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1956 );
1957 assert_eq!("99.99900", dec.to_string());
1958
1959 let mut dec = Decimal::from_str("-99.999").unwrap();
1960 dec.round_with_mode(
1961 -5,
1962 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1963 );
1964 assert_eq!("-99.99900", dec.to_string());
1965
1966 let mut dec = Decimal::from_str("-99.999").unwrap();
1967 dec.round_with_mode(
1968 -2,
1969 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1970 );
1971 assert_eq!("-100.00", dec.to_string());
1972
1973 let mut dec = Decimal::from_str("99.999").unwrap();
1974 dec.round_with_mode(
1975 4,
1976 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1977 );
1978 assert_eq!("0000", dec.to_string());
1979
1980 let mut dec = Decimal::from_str("-99.999").unwrap();
1981 dec.round_with_mode(
1982 4,
1983 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1984 );
1985 assert_eq!("-0000", dec.to_string());
1986
1987 let mut dec = Decimal::from_str("0.009").unwrap();
1988 dec.round_with_mode(
1989 -1,
1990 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1991 );
1992 assert_eq!("0.0", dec.to_string());
1993
1994 let mut dec = Decimal::from_str("-0.009").unwrap();
1995 dec.round_with_mode(
1996 -1,
1997 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
1998 );
1999 assert_eq!("-0.0", dec.to_string());
2000
2001 // Test specific cases
2002 let mut dec = Decimal::from_str("1.108").unwrap();
2003 dec.round_with_mode(
2004 -2,
2005 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
2006 );
2007 assert_eq!("1.11", dec.to_string());
2008
2009 let mut dec = Decimal::from_str("1.108").unwrap();
2010 dec.expand(-2);
2011 assert_eq!("1.11", dec.to_string());
2012
2013 let mut dec = Decimal::from_str("1.108").unwrap();
2014 dec.trunc(-2);
2015 assert_eq!("1.10", dec.to_string());
2016
2017 let mut dec = Decimal::from_str("2.78536913177").unwrap();
2018 dec.round_with_mode(
2019 -2,
2020 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
2021 );
2022 assert_eq!("2.79", dec.to_string());
2023 }
2024
2025 #[test]
2026 fn test_concatenate() {
2027 #[derive(Debug)]
2028 struct TestCase {
2029 pub input_1: &'static str,
2030 pub input_2: &'static str,
2031 pub expected: Option<&'static str>,
2032 }
2033 let cases = [
2034 TestCase {
2035 input_1: "123",
2036 input_2: "0.456",
2037 expected: Some("123.456"),
2038 },
2039 TestCase {
2040 input_1: "0.456",
2041 input_2: "123",
2042 expected: None,
2043 },
2044 TestCase {
2045 input_1: "123",
2046 input_2: "0.0456",
2047 expected: Some("123.0456"),
2048 },
2049 TestCase {
2050 input_1: "0.0456",
2051 input_2: "123",
2052 expected: None,
2053 },
2054 TestCase {
2055 input_1: "100",
2056 input_2: "0.456",
2057 expected: Some("100.456"),
2058 },
2059 TestCase {
2060 input_1: "0.456",
2061 input_2: "100",
2062 expected: None,
2063 },
2064 TestCase {
2065 input_1: "100",
2066 input_2: "0.001",
2067 expected: Some("100.001"),
2068 },
2069 TestCase {
2070 input_1: "0.001",
2071 input_2: "100",
2072 expected: None,
2073 },
2074 TestCase {
2075 input_1: "123000",
2076 input_2: "456",
2077 expected: Some("123456"),
2078 },
2079 TestCase {
2080 input_1: "456",
2081 input_2: "123000",
2082 expected: None,
2083 },
2084 TestCase {
2085 input_1: "5",
2086 input_2: "5",
2087 expected: None,
2088 },
2089 TestCase {
2090 input_1: "120",
2091 input_2: "25",
2092 expected: None,
2093 },
2094 TestCase {
2095 input_1: "1.1",
2096 input_2: "0.2",
2097 expected: None,
2098 },
2099 TestCase {
2100 input_1: "0",
2101 input_2: "222",
2102 expected: Some("222"),
2103 },
2104 TestCase {
2105 input_1: "222",
2106 input_2: "0",
2107 expected: Some("222"),
2108 },
2109 TestCase {
2110 input_1: "0",
2111 input_2: "0",
2112 expected: Some("0"),
2113 },
2114 TestCase {
2115 input_1: "000",
2116 input_2: "0",
2117 expected: Some("000"),
2118 },
2119 TestCase {
2120 input_1: "0.00",
2121 input_2: "0",
2122 expected: Some("0.00"),
2123 },
2124 ];
2125 for cas in &cases {
2126 let fd1 = Decimal::from_str(cas.input_1).unwrap();
2127 let fd2 = Decimal::from_str(cas.input_2).unwrap();
2128 match fd1.absolute.concatenated_end(fd2.absolute) {
2129 Ok(fd) => {
2130 assert_eq!(cas.expected, Some(fd.to_string().as_str()), "{cas:?}");
2131 }
2132 Err(_) => {
2133 assert!(cas.expected.is_none(), "{cas:?}");
2134 }
2135 }
2136 }
2137 }
2138
2139 #[test]
2140 fn test_rounding_increment() {
2141 // Test Truncate Right
2142 let mut dec = Decimal::from(4235970u32);
2143 dec.multiply_pow10(-3);
2144 assert_eq!("4235.970", dec.to_string());
2145
2146 dec.round_with_mode_and_increment(
2147 -2,
2148 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2149 RoundingIncrement::MultiplesOf2,
2150 );
2151 assert_eq!("4235.96", dec.to_string());
2152
2153 dec.round_with_mode_and_increment(
2154 -1,
2155 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2156 RoundingIncrement::MultiplesOf5,
2157 );
2158 assert_eq!("4235.5", dec.to_string());
2159
2160 dec.round_with_mode_and_increment(
2161 0,
2162 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2163 RoundingIncrement::MultiplesOf25,
2164 );
2165 assert_eq!("4225", dec.to_string());
2166
2167 dec.round_with_mode_and_increment(
2168 5,
2169 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2170 RoundingIncrement::MultiplesOf5,
2171 );
2172 assert_eq!("00000", dec.to_string());
2173
2174 dec.round_with_mode_and_increment(
2175 2,
2176 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2177 RoundingIncrement::MultiplesOf2,
2178 );
2179 assert_eq!("00000", dec.to_string());
2180
2181 let mut dec = Decimal::from_str("-99.999").unwrap();
2182 dec.round_with_mode_and_increment(
2183 -2,
2184 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2185 RoundingIncrement::MultiplesOf25,
2186 );
2187 assert_eq!("-99.75", dec.to_string());
2188
2189 let mut dec = Decimal::from_str("1234.56").unwrap();
2190 dec.round_with_mode_and_increment(
2191 -1,
2192 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2193 RoundingIncrement::MultiplesOf2,
2194 );
2195 assert_eq!("1234.4", dec.to_string());
2196
2197 let mut dec = Decimal::from_str("0.009").unwrap();
2198 dec.round_with_mode_and_increment(
2199 -1,
2200 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2201 RoundingIncrement::MultiplesOf5,
2202 );
2203 assert_eq!("0.0", dec.to_string());
2204
2205 let mut dec = Decimal::from_str("0.60").unwrap();
2206 dec.round_with_mode_and_increment(
2207 -2,
2208 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2209 RoundingIncrement::MultiplesOf25,
2210 );
2211 assert_eq!("0.50", dec.to_string());
2212
2213 let mut dec = Decimal::from_str("0.40").unwrap();
2214 dec.round_with_mode_and_increment(
2215 -2,
2216 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2217 RoundingIncrement::MultiplesOf25,
2218 );
2219 assert_eq!("0.25", dec.to_string());
2220
2221 let mut dec = Decimal::from_str("0.7000000099").unwrap();
2222 dec.round_with_mode_and_increment(
2223 -3,
2224 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2225 RoundingIncrement::MultiplesOf2,
2226 );
2227 assert_eq!("0.700", dec.to_string());
2228
2229 let mut dec = Decimal::from_str("5").unwrap();
2230 dec.round_with_mode_and_increment(
2231 0,
2232 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2233 RoundingIncrement::MultiplesOf25,
2234 );
2235 assert_eq!("0", dec.to_string());
2236
2237 let mut dec = Decimal::from(7u32);
2238 dec.multiply_pow10(i16::MIN);
2239 dec.round_with_mode_and_increment(
2240 i16::MIN,
2241 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2242 RoundingIncrement::MultiplesOf2,
2243 );
2244 let expected_min = {
2245 let mut expected_min = Decimal::from(6u32);
2246 expected_min.multiply_pow10(i16::MIN);
2247 expected_min
2248 };
2249 assert_eq!(expected_min, dec);
2250
2251 let mut dec = Decimal::from(9u32);
2252 dec.multiply_pow10(i16::MIN);
2253 dec.round_with_mode_and_increment(
2254 i16::MIN,
2255 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2256 RoundingIncrement::MultiplesOf5,
2257 );
2258
2259 let expected_min = {
2260 let mut expected_min = Decimal::from(5u32);
2261 expected_min.multiply_pow10(i16::MIN);
2262 expected_min
2263 };
2264
2265 assert_eq!(expected_min, dec);
2266
2267 let mut dec = Decimal::from(70u32);
2268 dec.multiply_pow10(i16::MIN);
2269 dec.round_with_mode_and_increment(
2270 i16::MIN,
2271 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2272 RoundingIncrement::MultiplesOf25,
2273 );
2274 let expected_min = {
2275 let mut expected_min = Decimal::from(50u32);
2276 expected_min.multiply_pow10(i16::MIN);
2277 expected_min
2278 };
2279 assert_eq!(expected_min, dec);
2280
2281 let mut dec = Decimal::from(7u32);
2282 dec.multiply_pow10(i16::MAX);
2283 dec.round_with_mode_and_increment(
2284 i16::MAX,
2285 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2286 RoundingIncrement::MultiplesOf2,
2287 );
2288 let expected_max = {
2289 let mut expected_max = Decimal::from(6u32);
2290 expected_max.multiply_pow10(i16::MAX);
2291 expected_max
2292 };
2293 assert_eq!(expected_max, dec);
2294
2295 let mut dec = Decimal::from(7u32);
2296 dec.multiply_pow10(i16::MAX);
2297 dec.round_with_mode_and_increment(
2298 i16::MAX,
2299 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2300 RoundingIncrement::MultiplesOf5,
2301 );
2302 let expected_max = {
2303 let mut expected_max = Decimal::from(5u32);
2304 expected_max.multiply_pow10(i16::MAX);
2305 expected_max
2306 };
2307 assert_eq!(expected_max, dec);
2308
2309 let mut dec = Decimal::from(7u32);
2310 dec.multiply_pow10(i16::MAX);
2311 dec.round_with_mode_and_increment(
2312 i16::MAX,
2313 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
2314 RoundingIncrement::MultiplesOf25,
2315 );
2316 let expected = {
2317 let mut expected = Decimal::from(0u32);
2318 expected.multiply_pow10(i16::MAX);
2319 expected
2320 };
2321 assert_eq!(expected, dec);
2322
2323 // Test Expand
2324 let mut dec = Decimal::from(4235970u32);
2325 dec.multiply_pow10(-3);
2326 assert_eq!("4235.970", dec.to_string());
2327
2328 dec.round_with_mode_and_increment(
2329 -2,
2330 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2331 RoundingIncrement::MultiplesOf2,
2332 );
2333 assert_eq!("4235.98", dec.to_string());
2334
2335 dec.round_with_mode_and_increment(
2336 -1,
2337 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2338 RoundingIncrement::MultiplesOf5,
2339 );
2340 assert_eq!("4236.0", dec.to_string());
2341
2342 dec.round_with_mode_and_increment(
2343 0,
2344 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2345 RoundingIncrement::MultiplesOf25,
2346 );
2347 assert_eq!("4250", dec.to_string());
2348
2349 dec.round_with_mode_and_increment(
2350 5,
2351 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2352 RoundingIncrement::MultiplesOf5,
2353 );
2354 assert_eq!("500000", dec.to_string());
2355
2356 dec.round_with_mode_and_increment(
2357 2,
2358 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2359 RoundingIncrement::MultiplesOf2,
2360 );
2361 assert_eq!("500000", dec.to_string());
2362
2363 let mut dec = Decimal::from_str("-99.999").unwrap();
2364 dec.round_with_mode_and_increment(
2365 -2,
2366 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2367 RoundingIncrement::MultiplesOf25,
2368 );
2369 assert_eq!("-100.00", dec.to_string());
2370
2371 let mut dec = Decimal::from_str("1234.56").unwrap();
2372 dec.round_with_mode_and_increment(
2373 -1,
2374 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2375 RoundingIncrement::MultiplesOf2,
2376 );
2377 assert_eq!("1234.6", dec.to_string());
2378
2379 let mut dec = Decimal::from_str("0.009").unwrap();
2380 dec.round_with_mode_and_increment(
2381 -1,
2382 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2383 RoundingIncrement::MultiplesOf5,
2384 );
2385 assert_eq!("0.5", dec.to_string());
2386
2387 let mut dec = Decimal::from_str("0.60").unwrap();
2388 dec.round_with_mode_and_increment(
2389 -2,
2390 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2391 RoundingIncrement::MultiplesOf25,
2392 );
2393 assert_eq!("0.75", dec.to_string());
2394
2395 let mut dec = Decimal::from_str("0.40").unwrap();
2396 dec.round_with_mode_and_increment(
2397 -2,
2398 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2399 RoundingIncrement::MultiplesOf25,
2400 );
2401 assert_eq!("0.50", dec.to_string());
2402
2403 let mut dec = Decimal::from_str("0.7000000099").unwrap();
2404 dec.round_with_mode_and_increment(
2405 -3,
2406 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2407 RoundingIncrement::MultiplesOf2,
2408 );
2409 assert_eq!("0.702", dec.to_string());
2410
2411 let mut dec = Decimal::from_str("5").unwrap();
2412 dec.round_with_mode_and_increment(
2413 0,
2414 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2415 RoundingIncrement::MultiplesOf25,
2416 );
2417 assert_eq!("25", dec.to_string());
2418
2419 let mut dec = Decimal::from(7u32);
2420 dec.multiply_pow10(i16::MIN);
2421 dec.round_with_mode_and_increment(
2422 i16::MIN,
2423 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2424 RoundingIncrement::MultiplesOf2,
2425 );
2426 let expected_min = {
2427 let mut expected_min = Decimal::from(8u32);
2428 expected_min.multiply_pow10(i16::MIN);
2429 expected_min
2430 };
2431 assert_eq!(expected_min, dec);
2432
2433 let mut dec = Decimal::from(9u32);
2434 dec.multiply_pow10(i16::MIN);
2435 dec.round_with_mode_and_increment(
2436 i16::MIN,
2437 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2438 RoundingIncrement::MultiplesOf5,
2439 );
2440 let expected_min = {
2441 let mut expected_min = Decimal::from(10u32);
2442 expected_min.multiply_pow10(i16::MIN);
2443 expected_min
2444 };
2445 assert_eq!(expected_min, dec);
2446
2447 let mut dec = Decimal::from(70u32);
2448 dec.multiply_pow10(i16::MIN);
2449 dec.round_with_mode_and_increment(
2450 i16::MIN,
2451 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2452 RoundingIncrement::MultiplesOf25,
2453 );
2454 let expected_min = {
2455 let mut expected_min = Decimal::from(75u32);
2456 expected_min.multiply_pow10(i16::MIN);
2457 expected_min
2458 };
2459 assert_eq!(expected_min, dec);
2460
2461 let mut dec = Decimal::from(7u32);
2462 dec.multiply_pow10(i16::MAX);
2463 dec.round_with_mode_and_increment(
2464 i16::MAX,
2465 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2466 RoundingIncrement::MultiplesOf2,
2467 );
2468 let expected_max = {
2469 let mut expected_max = Decimal::from(8u32);
2470 expected_max.multiply_pow10(i16::MAX);
2471 expected_max
2472 };
2473 assert_eq!(expected_max, dec);
2474
2475 let mut dec = Decimal::from(7u32);
2476 dec.multiply_pow10(i16::MAX);
2477 dec.round_with_mode_and_increment(
2478 i16::MAX,
2479 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2480 RoundingIncrement::MultiplesOf5,
2481 );
2482 let expected_max = {
2483 let mut expected_max = Decimal::from(0u32);
2484 expected_max.multiply_pow10(i16::MAX);
2485 expected_max
2486 };
2487 assert_eq!(expected_max, dec);
2488
2489 let mut dec = Decimal::from(7u32);
2490 dec.multiply_pow10(i16::MAX);
2491 dec.round_with_mode_and_increment(
2492 i16::MAX,
2493 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
2494 RoundingIncrement::MultiplesOf25,
2495 );
2496 let expected_max = {
2497 let mut expected_max = Decimal::from(0u32);
2498 expected_max.multiply_pow10(i16::MAX);
2499 expected_max
2500 };
2501 assert_eq!(expected_max, dec);
2502
2503 // Test Half Truncate Right
2504 let mut dec = Decimal::from(4235970u32);
2505 dec.multiply_pow10(-3);
2506 assert_eq!("4235.970", dec.to_string());
2507
2508 dec.round_with_mode_and_increment(
2509 -2,
2510 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2511 RoundingIncrement::MultiplesOf2,
2512 );
2513 assert_eq!("4235.96", dec.to_string());
2514
2515 dec.round_with_mode_and_increment(
2516 -1,
2517 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2518 RoundingIncrement::MultiplesOf5,
2519 );
2520 assert_eq!("4236.0", dec.to_string());
2521
2522 dec.round_with_mode_and_increment(
2523 0,
2524 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2525 RoundingIncrement::MultiplesOf25,
2526 );
2527 assert_eq!("4225", dec.to_string());
2528
2529 dec.round_with_mode_and_increment(
2530 5,
2531 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2532 RoundingIncrement::MultiplesOf5,
2533 );
2534 assert_eq!("00000", dec.to_string());
2535
2536 dec.round_with_mode_and_increment(
2537 2,
2538 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2539 RoundingIncrement::MultiplesOf2,
2540 );
2541 assert_eq!("00000", dec.to_string());
2542
2543 let mut dec = Decimal::from_str("-99.999").unwrap();
2544 dec.round_with_mode_and_increment(
2545 -2,
2546 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2547 RoundingIncrement::MultiplesOf25,
2548 );
2549 assert_eq!("-100.00", dec.to_string());
2550
2551 let mut dec = Decimal::from_str("1234.56").unwrap();
2552 dec.round_with_mode_and_increment(
2553 -1,
2554 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2555 RoundingIncrement::MultiplesOf2,
2556 );
2557 assert_eq!("1234.6", dec.to_string());
2558
2559 let mut dec = Decimal::from_str("0.009").unwrap();
2560 dec.round_with_mode_and_increment(
2561 -1,
2562 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2563 RoundingIncrement::MultiplesOf5,
2564 );
2565 assert_eq!("0.0", dec.to_string());
2566
2567 let mut dec = Decimal::from_str("0.60").unwrap();
2568 dec.round_with_mode_and_increment(
2569 -2,
2570 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2571 RoundingIncrement::MultiplesOf25,
2572 );
2573 assert_eq!("0.50", dec.to_string());
2574
2575 let mut dec = Decimal::from_str("0.40").unwrap();
2576 dec.round_with_mode_and_increment(
2577 -2,
2578 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2579 RoundingIncrement::MultiplesOf25,
2580 );
2581 assert_eq!("0.50", dec.to_string());
2582
2583 let mut dec = Decimal::from_str("0.7000000099").unwrap();
2584 dec.round_with_mode_and_increment(
2585 -3,
2586 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2587 RoundingIncrement::MultiplesOf2,
2588 );
2589 assert_eq!("0.700", dec.to_string());
2590
2591 let mut dec = Decimal::from_str("5").unwrap();
2592 dec.round_with_mode_and_increment(
2593 0,
2594 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2595 RoundingIncrement::MultiplesOf25,
2596 );
2597 assert_eq!("0", dec.to_string());
2598
2599 let mut dec = Decimal::from(7u32);
2600 dec.multiply_pow10(i16::MIN);
2601 dec.round_with_mode_and_increment(
2602 i16::MIN,
2603 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2604 RoundingIncrement::MultiplesOf2,
2605 );
2606 let expected_min = {
2607 let mut expected_min = Decimal::from(6u32);
2608 expected_min.multiply_pow10(i16::MIN);
2609 expected_min
2610 };
2611 assert_eq!(expected_min, dec);
2612
2613 let mut dec = Decimal::from(9u32);
2614 dec.multiply_pow10(i16::MIN);
2615 dec.round_with_mode_and_increment(
2616 i16::MIN,
2617 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2618 RoundingIncrement::MultiplesOf5,
2619 );
2620 let expected_min = {
2621 let mut expected_min = Decimal::from(10u32);
2622 expected_min.multiply_pow10(i16::MIN);
2623 expected_min
2624 };
2625 assert_eq!(expected_min, dec);
2626
2627 let mut dec = Decimal::from(70u32);
2628 dec.multiply_pow10(i16::MIN);
2629 dec.round_with_mode_and_increment(
2630 i16::MIN,
2631 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2632 RoundingIncrement::MultiplesOf25,
2633 );
2634 let expected_min = {
2635 let mut expected_min = Decimal::from(75u32);
2636 expected_min.multiply_pow10(i16::MIN);
2637 expected_min
2638 };
2639 assert_eq!(expected_min, dec);
2640
2641 let mut dec = Decimal::from(7u32);
2642 dec.multiply_pow10(i16::MAX);
2643 dec.round_with_mode_and_increment(
2644 i16::MAX,
2645 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2646 RoundingIncrement::MultiplesOf2,
2647 );
2648 let expected_max = {
2649 let mut expected_max = Decimal::from(6u32);
2650 expected_max.multiply_pow10(i16::MAX);
2651 expected_max
2652 };
2653 assert_eq!(expected_max, dec);
2654
2655 let mut dec = Decimal::from(7u32);
2656 dec.multiply_pow10(i16::MAX);
2657 dec.round_with_mode_and_increment(
2658 i16::MAX,
2659 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2660 RoundingIncrement::MultiplesOf5,
2661 );
2662 let expected_max = {
2663 let mut expected_max = Decimal::from(5u32);
2664 expected_max.multiply_pow10(i16::MAX);
2665 expected_max
2666 };
2667 assert_eq!(expected_max, dec);
2668
2669 let mut dec = Decimal::from(7u32);
2670 dec.multiply_pow10(i16::MAX);
2671 dec.round_with_mode_and_increment(
2672 i16::MAX,
2673 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
2674 RoundingIncrement::MultiplesOf25,
2675 );
2676 let expected_max = {
2677 let mut expected_max = Decimal::from(0u32);
2678 expected_max.multiply_pow10(i16::MAX);
2679 expected_max
2680 };
2681 assert_eq!(expected_max, dec);
2682
2683 // Test Half Expand
2684 let mut dec = Decimal::from(4235970u32);
2685 dec.multiply_pow10(-3);
2686 assert_eq!("4235.970", dec.to_string());
2687
2688 dec.round_with_mode_and_increment(
2689 -2,
2690 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2691 RoundingIncrement::MultiplesOf2,
2692 );
2693 assert_eq!("4235.98", dec.to_string());
2694
2695 dec.round_with_mode_and_increment(
2696 -1,
2697 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2698 RoundingIncrement::MultiplesOf5,
2699 );
2700 assert_eq!("4236.0", dec.to_string());
2701
2702 dec.round_with_mode_and_increment(
2703 0,
2704 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2705 RoundingIncrement::MultiplesOf25,
2706 );
2707 assert_eq!("4225", dec.to_string());
2708
2709 dec.round_with_mode_and_increment(
2710 5,
2711 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2712 RoundingIncrement::MultiplesOf5,
2713 );
2714 assert_eq!("00000", dec.to_string());
2715
2716 dec.round_with_mode_and_increment(
2717 2,
2718 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2719 RoundingIncrement::MultiplesOf2,
2720 );
2721 assert_eq!("00000", dec.to_string());
2722
2723 let mut dec = Decimal::from_str("-99.999").unwrap();
2724 dec.round_with_mode_and_increment(
2725 -2,
2726 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2727 RoundingIncrement::MultiplesOf25,
2728 );
2729 assert_eq!("-100.00", dec.to_string());
2730
2731 let mut dec = Decimal::from_str("1234.56").unwrap();
2732 dec.round_with_mode_and_increment(
2733 -1,
2734 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2735 RoundingIncrement::MultiplesOf2,
2736 );
2737 assert_eq!("1234.6", dec.to_string());
2738
2739 let mut dec = Decimal::from_str("0.009").unwrap();
2740 dec.round_with_mode_and_increment(
2741 -1,
2742 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2743 RoundingIncrement::MultiplesOf5,
2744 );
2745 assert_eq!("0.0", dec.to_string());
2746
2747 let mut dec = Decimal::from_str("0.60").unwrap();
2748 dec.round_with_mode_and_increment(
2749 -2,
2750 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2751 RoundingIncrement::MultiplesOf25,
2752 );
2753 assert_eq!("0.50", dec.to_string());
2754
2755 let mut dec = Decimal::from_str("0.40").unwrap();
2756 dec.round_with_mode_and_increment(
2757 -2,
2758 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2759 RoundingIncrement::MultiplesOf25,
2760 );
2761 assert_eq!("0.50", dec.to_string());
2762
2763 let mut dec = Decimal::from_str("0.7000000099").unwrap();
2764 dec.round_with_mode_and_increment(
2765 -3,
2766 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2767 RoundingIncrement::MultiplesOf2,
2768 );
2769 assert_eq!("0.700", dec.to_string());
2770
2771 let mut dec = Decimal::from_str("5").unwrap();
2772 dec.round_with_mode_and_increment(
2773 0,
2774 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2775 RoundingIncrement::MultiplesOf25,
2776 );
2777 assert_eq!("0", dec.to_string());
2778
2779 let mut dec = Decimal::from(7u32);
2780 dec.multiply_pow10(i16::MIN);
2781 dec.round_with_mode_and_increment(
2782 i16::MIN,
2783 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2784 RoundingIncrement::MultiplesOf2,
2785 );
2786 let expected_min = {
2787 let mut expected_min = Decimal::from(8u32);
2788 expected_min.multiply_pow10(i16::MIN);
2789 expected_min
2790 };
2791 assert_eq!(expected_min, dec);
2792
2793 let mut dec = Decimal::from(9u32);
2794 dec.multiply_pow10(i16::MIN);
2795 dec.round_with_mode_and_increment(
2796 i16::MIN,
2797 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2798 RoundingIncrement::MultiplesOf5,
2799 );
2800 let expected_min = {
2801 let mut expected_min = Decimal::from(10u32);
2802 expected_min.multiply_pow10(i16::MIN);
2803 expected_min
2804 };
2805 assert_eq!(expected_min, dec);
2806
2807 let mut dec = Decimal::from(70u32);
2808 dec.multiply_pow10(i16::MIN);
2809 dec.round_with_mode_and_increment(
2810 i16::MIN,
2811 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2812 RoundingIncrement::MultiplesOf25,
2813 );
2814 let expected_min = {
2815 let mut expected_min = Decimal::from(75u32);
2816 expected_min.multiply_pow10(i16::MIN);
2817 expected_min
2818 };
2819 assert_eq!(expected_min, dec);
2820
2821 let mut dec = Decimal::from(7u32);
2822 dec.multiply_pow10(i16::MAX);
2823 dec.round_with_mode_and_increment(
2824 i16::MAX,
2825 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
2826 RoundingIncrement::MultiplesOf2,
2827 );
2828 let expected_max = {
2829 let mut expected_max = Decimal::from(8u32);
2830 expected_max.multiply_pow10(i16::MAX);
2831 expected_max
2832 };
2833 assert_eq!(expected_max, dec);
2834
2835 // Test Ceil
2836 let mut dec = Decimal::from(4235970u32);
2837 dec.multiply_pow10(-3);
2838 assert_eq!("4235.970", dec.to_string());
2839
2840 dec.round_with_mode_and_increment(
2841 -2,
2842 SignedRoundingMode::Ceil,
2843 RoundingIncrement::MultiplesOf2,
2844 );
2845 assert_eq!("4235.98", dec.to_string());
2846
2847 dec.round_with_mode_and_increment(
2848 -1,
2849 SignedRoundingMode::Ceil,
2850 RoundingIncrement::MultiplesOf5,
2851 );
2852 assert_eq!("4236.0", dec.to_string());
2853
2854 dec.round_with_mode_and_increment(
2855 0,
2856 SignedRoundingMode::Ceil,
2857 RoundingIncrement::MultiplesOf25,
2858 );
2859 assert_eq!("4250", dec.to_string());
2860
2861 dec.round_with_mode_and_increment(5, SignedRoundingMode::Ceil, RoundingIncrement::MultiplesOf5);
2862 assert_eq!("500000", dec.to_string());
2863
2864 dec.round_with_mode_and_increment(2, SignedRoundingMode::Ceil, RoundingIncrement::MultiplesOf2);
2865 assert_eq!("500000", dec.to_string());
2866
2867 let mut dec = Decimal::from_str("-99.999").unwrap();
2868 dec.round_with_mode_and_increment(
2869 -2,
2870 SignedRoundingMode::Ceil,
2871 RoundingIncrement::MultiplesOf25,
2872 );
2873 assert_eq!("-99.75", dec.to_string());
2874
2875 let mut dec = Decimal::from_str("1234.56").unwrap();
2876 dec.round_with_mode_and_increment(
2877 -1,
2878 SignedRoundingMode::Ceil,
2879 RoundingIncrement::MultiplesOf2,
2880 );
2881 assert_eq!("1234.6", dec.to_string());
2882
2883 let mut dec = Decimal::from_str("0.009").unwrap();
2884 dec.round_with_mode_and_increment(
2885 -1,
2886 SignedRoundingMode::Ceil,
2887 RoundingIncrement::MultiplesOf5,
2888 );
2889 assert_eq!("0.5", dec.to_string());
2890
2891 let mut dec = Decimal::from_str("0.60").unwrap();
2892 dec.round_with_mode_and_increment(
2893 -2,
2894 SignedRoundingMode::Ceil,
2895 RoundingIncrement::MultiplesOf25,
2896 );
2897 assert_eq!("0.75", dec.to_string());
2898
2899 let mut dec = Decimal::from_str("0.40").unwrap();
2900 dec.round_with_mode_and_increment(
2901 -2,
2902 SignedRoundingMode::Ceil,
2903 RoundingIncrement::MultiplesOf25,
2904 );
2905 assert_eq!("0.50", dec.to_string());
2906
2907 let mut dec = Decimal::from_str("0.7000000099").unwrap();
2908 dec.round_with_mode_and_increment(
2909 -3,
2910 SignedRoundingMode::Ceil,
2911 RoundingIncrement::MultiplesOf2,
2912 );
2913 assert_eq!("0.702", dec.to_string());
2914
2915 let mut dec = Decimal::from_str("5").unwrap();
2916 dec.round_with_mode_and_increment(
2917 0,
2918 SignedRoundingMode::Ceil,
2919 RoundingIncrement::MultiplesOf25,
2920 );
2921 assert_eq!("25", dec.to_string());
2922
2923 let mut dec = Decimal::from(7u32);
2924 dec.multiply_pow10(i16::MIN);
2925 dec.round_with_mode_and_increment(
2926 i16::MIN,
2927 SignedRoundingMode::Ceil,
2928 RoundingIncrement::MultiplesOf2,
2929 );
2930 let expected_min = {
2931 let mut expected_min = Decimal::from(8u32);
2932 expected_min.multiply_pow10(i16::MIN);
2933 expected_min
2934 };
2935 assert_eq!(expected_min, dec);
2936
2937 let mut dec = Decimal::from(9u32);
2938 dec.multiply_pow10(i16::MIN);
2939 dec.round_with_mode_and_increment(
2940 i16::MIN,
2941 SignedRoundingMode::Ceil,
2942 RoundingIncrement::MultiplesOf5,
2943 );
2944 let expected_min = {
2945 let mut expected_min = Decimal::from(10u32);
2946 expected_min.multiply_pow10(i16::MIN);
2947 expected_min
2948 };
2949 assert_eq!(expected_min, dec);
2950
2951 let mut dec = Decimal::from(70u32);
2952 dec.multiply_pow10(i16::MIN);
2953 dec.round_with_mode_and_increment(
2954 i16::MIN,
2955 SignedRoundingMode::Ceil,
2956 RoundingIncrement::MultiplesOf25,
2957 );
2958 let expected_min = {
2959 let mut expected_min = Decimal::from(75u32);
2960 expected_min.multiply_pow10(i16::MIN);
2961 expected_min
2962 };
2963 assert_eq!(expected_min, dec);
2964
2965 let mut dec = Decimal::from(7u32);
2966 dec.multiply_pow10(i16::MAX);
2967 dec.round_with_mode_and_increment(
2968 i16::MAX,
2969 SignedRoundingMode::Ceil,
2970 RoundingIncrement::MultiplesOf2,
2971 );
2972 let expected_max = {
2973 let mut expected_max = Decimal::from(8u32);
2974 expected_max.multiply_pow10(i16::MAX);
2975 expected_max
2976 };
2977 assert_eq!(expected_max, dec);
2978
2979 // Test Half Ceil
2980 let mut dec = Decimal::from(4235970u32);
2981 dec.multiply_pow10(-3);
2982 assert_eq!("4235.970", dec.to_string());
2983
2984 dec.round_with_mode_and_increment(
2985 -2,
2986 SignedRoundingMode::HalfCeil,
2987 RoundingIncrement::MultiplesOf2,
2988 );
2989 assert_eq!("4235.98", dec.to_string());
2990
2991 dec.round_with_mode_and_increment(
2992 -1,
2993 SignedRoundingMode::HalfCeil,
2994 RoundingIncrement::MultiplesOf5,
2995 );
2996 assert_eq!("4236.0", dec.to_string());
2997
2998 dec.round_with_mode_and_increment(
2999 0,
3000 SignedRoundingMode::HalfCeil,
3001 RoundingIncrement::MultiplesOf25,
3002 );
3003 assert_eq!("4225", dec.to_string());
3004
3005 dec.round_with_mode_and_increment(
3006 5,
3007 SignedRoundingMode::HalfCeil,
3008 RoundingIncrement::MultiplesOf5,
3009 );
3010 assert_eq!("00000", dec.to_string());
3011
3012 dec.round_with_mode_and_increment(
3013 2,
3014 SignedRoundingMode::HalfCeil,
3015 RoundingIncrement::MultiplesOf2,
3016 );
3017 assert_eq!("00000", dec.to_string());
3018
3019 let mut dec = Decimal::from_str("-99.999").unwrap();
3020 dec.round_with_mode_and_increment(
3021 -2,
3022 SignedRoundingMode::HalfCeil,
3023 RoundingIncrement::MultiplesOf25,
3024 );
3025 assert_eq!("-100.00", dec.to_string());
3026
3027 let mut dec = Decimal::from_str("1234.56").unwrap();
3028 dec.round_with_mode_and_increment(
3029 -1,
3030 SignedRoundingMode::HalfCeil,
3031 RoundingIncrement::MultiplesOf2,
3032 );
3033 assert_eq!("1234.6", dec.to_string());
3034
3035 let mut dec = Decimal::from_str("0.009").unwrap();
3036 dec.round_with_mode_and_increment(
3037 -1,
3038 SignedRoundingMode::HalfCeil,
3039 RoundingIncrement::MultiplesOf5,
3040 );
3041 assert_eq!("0.0", dec.to_string());
3042
3043 let mut dec = Decimal::from_str("0.60").unwrap();
3044 dec.round_with_mode_and_increment(
3045 -2,
3046 SignedRoundingMode::HalfCeil,
3047 RoundingIncrement::MultiplesOf25,
3048 );
3049 assert_eq!("0.50", dec.to_string());
3050
3051 let mut dec = Decimal::from_str("0.40").unwrap();
3052 dec.round_with_mode_and_increment(
3053 -2,
3054 SignedRoundingMode::HalfCeil,
3055 RoundingIncrement::MultiplesOf25,
3056 );
3057 assert_eq!("0.50", dec.to_string());
3058
3059 let mut dec = Decimal::from_str("0.7000000099").unwrap();
3060 dec.round_with_mode_and_increment(
3061 -3,
3062 SignedRoundingMode::HalfCeil,
3063 RoundingIncrement::MultiplesOf2,
3064 );
3065 assert_eq!("0.700", dec.to_string());
3066
3067 let mut dec = Decimal::from_str("5").unwrap();
3068 dec.round_with_mode_and_increment(
3069 0,
3070 SignedRoundingMode::HalfCeil,
3071 RoundingIncrement::MultiplesOf25,
3072 );
3073 assert_eq!("0", dec.to_string());
3074
3075 let mut dec = Decimal::from(7u32);
3076 dec.multiply_pow10(i16::MIN);
3077 dec.round_with_mode_and_increment(
3078 i16::MIN,
3079 SignedRoundingMode::HalfCeil,
3080 RoundingIncrement::MultiplesOf2,
3081 );
3082 let expected_min = {
3083 let mut expected_min = Decimal::from(8u32);
3084 expected_min.multiply_pow10(i16::MIN);
3085 expected_min
3086 };
3087 assert_eq!(expected_min, dec);
3088
3089 let mut dec = Decimal::from(9u32);
3090 dec.multiply_pow10(i16::MIN);
3091 dec.round_with_mode_and_increment(
3092 i16::MIN,
3093 SignedRoundingMode::HalfCeil,
3094 RoundingIncrement::MultiplesOf5,
3095 );
3096 let expected_min = {
3097 let mut expected_min = Decimal::from(10u32);
3098 expected_min.multiply_pow10(i16::MIN);
3099 expected_min
3100 };
3101 assert_eq!(expected_min, dec);
3102
3103 let mut dec = Decimal::from(70u32);
3104 dec.multiply_pow10(i16::MIN);
3105 dec.round_with_mode_and_increment(
3106 i16::MIN,
3107 SignedRoundingMode::HalfCeil,
3108 RoundingIncrement::MultiplesOf25,
3109 );
3110 let expected_min = {
3111 let mut expected_min = Decimal::from(75u32);
3112 expected_min.multiply_pow10(i16::MIN);
3113 expected_min
3114 };
3115 assert_eq!(expected_min, dec);
3116
3117 let mut dec = Decimal::from(7u32);
3118 dec.multiply_pow10(i16::MAX);
3119 dec.round_with_mode_and_increment(
3120 i16::MAX,
3121 SignedRoundingMode::HalfCeil,
3122 RoundingIncrement::MultiplesOf2,
3123 );
3124 let expected_max = {
3125 let mut expected_max = Decimal::from(8u32);
3126 expected_max.multiply_pow10(i16::MAX);
3127 expected_max
3128 };
3129 assert_eq!(expected_max, dec);
3130
3131 // Test Floor
3132 let mut dec = Decimal::from(4235970u32);
3133 dec.multiply_pow10(-3);
3134 assert_eq!("4235.970", dec.to_string());
3135
3136 dec.round_with_mode_and_increment(
3137 -2,
3138 SignedRoundingMode::Floor,
3139 RoundingIncrement::MultiplesOf2,
3140 );
3141 assert_eq!("4235.96", dec.to_string());
3142
3143 dec.round_with_mode_and_increment(
3144 -1,
3145 SignedRoundingMode::Floor,
3146 RoundingIncrement::MultiplesOf5,
3147 );
3148 assert_eq!("4235.5", dec.to_string());
3149
3150 dec.round_with_mode_and_increment(
3151 0,
3152 SignedRoundingMode::Floor,
3153 RoundingIncrement::MultiplesOf25,
3154 );
3155 assert_eq!("4225", dec.to_string());
3156
3157 dec.round_with_mode_and_increment(
3158 5,
3159 SignedRoundingMode::Floor,
3160 RoundingIncrement::MultiplesOf5,
3161 );
3162 assert_eq!("00000", dec.to_string());
3163
3164 dec.round_with_mode_and_increment(
3165 2,
3166 SignedRoundingMode::Floor,
3167 RoundingIncrement::MultiplesOf2,
3168 );
3169 assert_eq!("00000", dec.to_string());
3170
3171 let mut dec = Decimal::from_str("-99.999").unwrap();
3172 dec.round_with_mode_and_increment(
3173 -2,
3174 SignedRoundingMode::Floor,
3175 RoundingIncrement::MultiplesOf25,
3176 );
3177 assert_eq!("-100.00", dec.to_string());
3178
3179 let mut dec = Decimal::from_str("1234.56").unwrap();
3180 dec.round_with_mode_and_increment(
3181 -1,
3182 SignedRoundingMode::Floor,
3183 RoundingIncrement::MultiplesOf2,
3184 );
3185 assert_eq!("1234.4", dec.to_string());
3186
3187 let mut dec = Decimal::from_str("0.009").unwrap();
3188 dec.round_with_mode_and_increment(
3189 -1,
3190 SignedRoundingMode::Floor,
3191 RoundingIncrement::MultiplesOf5,
3192 );
3193 assert_eq!("0.0", dec.to_string());
3194
3195 let mut dec = Decimal::from_str("0.60").unwrap();
3196 dec.round_with_mode_and_increment(
3197 -2,
3198 SignedRoundingMode::Floor,
3199 RoundingIncrement::MultiplesOf25,
3200 );
3201 assert_eq!("0.50", dec.to_string());
3202
3203 let mut dec = Decimal::from_str("0.40").unwrap();
3204 dec.round_with_mode_and_increment(
3205 -2,
3206 SignedRoundingMode::Floor,
3207 RoundingIncrement::MultiplesOf25,
3208 );
3209 assert_eq!("0.25", dec.to_string());
3210
3211 let mut dec = Decimal::from_str("0.7000000099").unwrap();
3212 dec.round_with_mode_and_increment(
3213 -3,
3214 SignedRoundingMode::Floor,
3215 RoundingIncrement::MultiplesOf2,
3216 );
3217 assert_eq!("0.700", dec.to_string());
3218
3219 let mut dec = Decimal::from_str("5").unwrap();
3220 dec.round_with_mode_and_increment(
3221 0,
3222 SignedRoundingMode::Floor,
3223 RoundingIncrement::MultiplesOf25,
3224 );
3225 assert_eq!("0", dec.to_string());
3226
3227 let mut dec = Decimal::from(7u32);
3228 dec.multiply_pow10(i16::MIN);
3229 dec.round_with_mode_and_increment(
3230 i16::MIN,
3231 SignedRoundingMode::Floor,
3232 RoundingIncrement::MultiplesOf2,
3233 );
3234 let expected_min = {
3235 let mut expected_min = Decimal::from(6u32);
3236 expected_min.multiply_pow10(i16::MIN);
3237 expected_min
3238 };
3239 assert_eq!(expected_min, dec);
3240
3241 let mut dec = Decimal::from(9u32);
3242 dec.multiply_pow10(i16::MIN);
3243 dec.round_with_mode_and_increment(
3244 i16::MIN,
3245 SignedRoundingMode::Floor,
3246 RoundingIncrement::MultiplesOf5,
3247 );
3248 let expected_min = {
3249 let mut expected_min = Decimal::from(5u32);
3250 expected_min.multiply_pow10(i16::MIN);
3251 expected_min
3252 };
3253 assert_eq!(expected_min, dec);
3254
3255 let mut dec = Decimal::from(70u32);
3256 dec.multiply_pow10(i16::MIN);
3257 dec.round_with_mode_and_increment(
3258 i16::MIN,
3259 SignedRoundingMode::Floor,
3260 RoundingIncrement::MultiplesOf25,
3261 );
3262 let expected_min = {
3263 let mut expected_min = Decimal::from(50u32);
3264 expected_min.multiply_pow10(i16::MIN);
3265 expected_min
3266 };
3267 assert_eq!(expected_min, dec);
3268
3269 let mut dec = Decimal::from(7u32);
3270 dec.multiply_pow10(i16::MAX);
3271 dec.round_with_mode_and_increment(
3272 i16::MAX,
3273 SignedRoundingMode::Floor,
3274 RoundingIncrement::MultiplesOf2,
3275 );
3276 let expected_max = {
3277 let mut expected_max = Decimal::from(6u32);
3278 expected_max.multiply_pow10(i16::MAX);
3279 expected_max
3280 };
3281 assert_eq!(expected_max, dec);
3282
3283 // Test Half Floor
3284 let mut dec = Decimal::from(4235970u32);
3285 dec.multiply_pow10(-3);
3286 assert_eq!("4235.970", dec.to_string());
3287
3288 dec.round_with_mode_and_increment(
3289 -2,
3290 SignedRoundingMode::HalfFloor,
3291 RoundingIncrement::MultiplesOf2,
3292 );
3293 assert_eq!("4235.96", dec.to_string());
3294
3295 dec.round_with_mode_and_increment(
3296 -1,
3297 SignedRoundingMode::HalfFloor,
3298 RoundingIncrement::MultiplesOf5,
3299 );
3300 assert_eq!("4236.0", dec.to_string());
3301
3302 dec.round_with_mode_and_increment(
3303 0,
3304 SignedRoundingMode::HalfFloor,
3305 RoundingIncrement::MultiplesOf25,
3306 );
3307 assert_eq!("4225", dec.to_string());
3308
3309 dec.round_with_mode_and_increment(
3310 5,
3311 SignedRoundingMode::HalfFloor,
3312 RoundingIncrement::MultiplesOf5,
3313 );
3314 assert_eq!("00000", dec.to_string());
3315
3316 dec.round_with_mode_and_increment(
3317 2,
3318 SignedRoundingMode::HalfFloor,
3319 RoundingIncrement::MultiplesOf2,
3320 );
3321 assert_eq!("00000", dec.to_string());
3322
3323 let mut dec = Decimal::from_str("-99.999").unwrap();
3324 dec.round_with_mode_and_increment(
3325 -2,
3326 SignedRoundingMode::HalfFloor,
3327 RoundingIncrement::MultiplesOf25,
3328 );
3329 assert_eq!("-100.00", dec.to_string());
3330
3331 let mut dec = Decimal::from_str("1234.56").unwrap();
3332 dec.round_with_mode_and_increment(
3333 -1,
3334 SignedRoundingMode::HalfFloor,
3335 RoundingIncrement::MultiplesOf2,
3336 );
3337 assert_eq!("1234.6", dec.to_string());
3338
3339 let mut dec = Decimal::from_str("0.009").unwrap();
3340 dec.round_with_mode_and_increment(
3341 -1,
3342 SignedRoundingMode::HalfFloor,
3343 RoundingIncrement::MultiplesOf5,
3344 );
3345 assert_eq!("0.0", dec.to_string());
3346
3347 let mut dec = Decimal::from_str("0.60").unwrap();
3348 dec.round_with_mode_and_increment(
3349 -2,
3350 SignedRoundingMode::HalfFloor,
3351 RoundingIncrement::MultiplesOf25,
3352 );
3353 assert_eq!("0.50", dec.to_string());
3354
3355 let mut dec = Decimal::from_str("0.40").unwrap();
3356 dec.round_with_mode_and_increment(
3357 -2,
3358 SignedRoundingMode::HalfFloor,
3359 RoundingIncrement::MultiplesOf25,
3360 );
3361 assert_eq!("0.50", dec.to_string());
3362
3363 let mut dec = Decimal::from_str("0.7000000099").unwrap();
3364 dec.round_with_mode_and_increment(
3365 -3,
3366 SignedRoundingMode::HalfFloor,
3367 RoundingIncrement::MultiplesOf2,
3368 );
3369 assert_eq!("0.700", dec.to_string());
3370
3371 let mut dec = Decimal::from_str("5").unwrap();
3372 dec.round_with_mode_and_increment(
3373 0,
3374 SignedRoundingMode::HalfFloor,
3375 RoundingIncrement::MultiplesOf25,
3376 );
3377 assert_eq!("0", dec.to_string());
3378
3379 let mut dec = Decimal::from(7u32);
3380 dec.multiply_pow10(i16::MIN);
3381 dec.round_with_mode_and_increment(
3382 i16::MIN,
3383 SignedRoundingMode::HalfFloor,
3384 RoundingIncrement::MultiplesOf2,
3385 );
3386 let expected_min = {
3387 let mut expected_min = Decimal::from(6u32);
3388 expected_min.multiply_pow10(i16::MIN);
3389 expected_min
3390 };
3391 assert_eq!(expected_min, dec);
3392
3393 let mut dec = Decimal::from(9u32);
3394 dec.multiply_pow10(i16::MIN);
3395 dec.round_with_mode_and_increment(
3396 i16::MIN,
3397 SignedRoundingMode::HalfFloor,
3398 RoundingIncrement::MultiplesOf5,
3399 );
3400 let expected_min = {
3401 let mut expected_min = Decimal::from(10u32);
3402 expected_min.multiply_pow10(i16::MIN);
3403 expected_min
3404 };
3405 assert_eq!(expected_min, dec);
3406
3407 let mut dec = Decimal::from(70u32);
3408 dec.multiply_pow10(i16::MIN);
3409 dec.round_with_mode_and_increment(
3410 i16::MIN,
3411 SignedRoundingMode::HalfFloor,
3412 RoundingIncrement::MultiplesOf25,
3413 );
3414 let expected_min = {
3415 let mut expected_min = Decimal::from(75u32);
3416 expected_min.multiply_pow10(i16::MIN);
3417 expected_min
3418 };
3419 assert_eq!(expected_min, dec);
3420
3421 let mut dec = Decimal::from(7u32);
3422 dec.multiply_pow10(i16::MAX);
3423 dec.round_with_mode_and_increment(
3424 i16::MAX,
3425 SignedRoundingMode::HalfFloor,
3426 RoundingIncrement::MultiplesOf2,
3427 );
3428 let expected_max = {
3429 let mut expected_max = Decimal::from(6u32);
3430 expected_max.multiply_pow10(i16::MAX);
3431 expected_max
3432 };
3433 assert_eq!(expected_max, dec);
3434
3435 // Test Half Even
3436 let mut dec = Decimal::from(4235970u32);
3437 dec.multiply_pow10(-3);
3438 assert_eq!("4235.970", dec.to_string());
3439
3440 dec.round_with_mode_and_increment(
3441 -2,
3442 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3443 RoundingIncrement::MultiplesOf2,
3444 );
3445 assert_eq!("4235.96", dec.to_string());
3446
3447 dec.round_with_mode_and_increment(
3448 -1,
3449 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3450 RoundingIncrement::MultiplesOf5,
3451 );
3452 assert_eq!("4236.0", dec.to_string());
3453
3454 dec.round_with_mode_and_increment(
3455 0,
3456 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3457 RoundingIncrement::MultiplesOf25,
3458 );
3459 assert_eq!("4225", dec.to_string());
3460
3461 dec.round_with_mode_and_increment(
3462 5,
3463 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3464 RoundingIncrement::MultiplesOf5,
3465 );
3466 assert_eq!("00000", dec.to_string());
3467
3468 dec.round_with_mode_and_increment(
3469 2,
3470 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3471 RoundingIncrement::MultiplesOf2,
3472 );
3473 assert_eq!("00000", dec.to_string());
3474
3475 let mut dec = Decimal::from_str("-99.999").unwrap();
3476 dec.round_with_mode_and_increment(
3477 -2,
3478 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3479 RoundingIncrement::MultiplesOf25,
3480 );
3481 assert_eq!("-100.00", dec.to_string());
3482
3483 let mut dec = Decimal::from_str("1234.56").unwrap();
3484 dec.round_with_mode_and_increment(
3485 -1,
3486 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3487 RoundingIncrement::MultiplesOf2,
3488 );
3489 assert_eq!("1234.6", dec.to_string());
3490
3491 let mut dec = Decimal::from_str("0.009").unwrap();
3492 dec.round_with_mode_and_increment(
3493 -1,
3494 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3495 RoundingIncrement::MultiplesOf5,
3496 );
3497 assert_eq!("0.0", dec.to_string());
3498
3499 let mut dec = Decimal::from_str("0.60").unwrap();
3500 dec.round_with_mode_and_increment(
3501 -2,
3502 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3503 RoundingIncrement::MultiplesOf25,
3504 );
3505 assert_eq!("0.50", dec.to_string());
3506
3507 let mut dec = Decimal::from_str("0.40").unwrap();
3508 dec.round_with_mode_and_increment(
3509 -2,
3510 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3511 RoundingIncrement::MultiplesOf25,
3512 );
3513 assert_eq!("0.50", dec.to_string());
3514
3515 let mut dec = Decimal::from_str("0.7000000099").unwrap();
3516 dec.round_with_mode_and_increment(
3517 -3,
3518 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3519 RoundingIncrement::MultiplesOf2,
3520 );
3521 assert_eq!("0.700", dec.to_string());
3522
3523 let mut dec = Decimal::from_str("5").unwrap();
3524 dec.round_with_mode_and_increment(
3525 0,
3526 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3527 RoundingIncrement::MultiplesOf25,
3528 );
3529 assert_eq!("0", dec.to_string());
3530
3531 let mut dec = Decimal::from(7u32);
3532 dec.multiply_pow10(i16::MIN);
3533 dec.round_with_mode_and_increment(
3534 i16::MIN,
3535 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3536 RoundingIncrement::MultiplesOf2,
3537 );
3538 let expected_min = {
3539 let mut expected_min = Decimal::from(8u32);
3540 expected_min.multiply_pow10(i16::MIN);
3541 expected_min
3542 };
3543 assert_eq!(expected_min, dec);
3544
3545 let mut dec = Decimal::from(9u32);
3546 dec.multiply_pow10(i16::MIN);
3547 dec.round_with_mode_and_increment(
3548 i16::MIN,
3549 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3550 RoundingIncrement::MultiplesOf5,
3551 );
3552 let expected_min = {
3553 let mut expected_min = Decimal::from(10u32);
3554 expected_min.multiply_pow10(i16::MIN);
3555 expected_min
3556 };
3557 assert_eq!(expected_min, dec);
3558
3559 let mut dec = Decimal::from(70u32);
3560 dec.multiply_pow10(i16::MIN);
3561 dec.round_with_mode_and_increment(
3562 i16::MIN,
3563 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3564 RoundingIncrement::MultiplesOf25,
3565 );
3566 let expected_min = {
3567 let mut expected_min = Decimal::from(75u32);
3568 expected_min.multiply_pow10(i16::MIN);
3569 expected_min
3570 };
3571 assert_eq!(expected_min, dec);
3572
3573 let mut dec = Decimal::from(7u32);
3574 dec.multiply_pow10(i16::MAX);
3575 dec.round_with_mode_and_increment(
3576 i16::MAX,
3577 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3578 RoundingIncrement::MultiplesOf2,
3579 );
3580 let expected_max = {
3581 let mut expected_max = Decimal::from(8u32);
3582 expected_max.multiply_pow10(i16::MAX);
3583 expected_max
3584 };
3585 assert_eq!(expected_max, dec);
3586
3587 // Test specific cases
3588 let mut dec = Decimal::from_str("1.108").unwrap();
3589 dec.round_with_mode_and_increment(
3590 -2,
3591 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3592 RoundingIncrement::MultiplesOf2,
3593 );
3594 assert_eq!("1.12", dec.to_string());
3595
3596 let mut dec = Decimal::from_str("1.108").unwrap();
3597 dec.round_with_mode_and_increment(
3598 -2,
3599 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3600 RoundingIncrement::MultiplesOf5,
3601 );
3602 assert_eq!("1.15", dec.to_string());
3603
3604 let mut dec = Decimal::from_str("1.108").unwrap();
3605 dec.round_with_mode_and_increment(
3606 -2,
3607 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3608 RoundingIncrement::MultiplesOf25,
3609 );
3610 assert_eq!("1.25", dec.to_string());
3611
3612 let mut dec = Decimal::from(9u32);
3613 dec.multiply_pow10(i16::MAX - 1);
3614 dec.round_with_mode_and_increment(
3615 i16::MAX - 1,
3616 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3617 RoundingIncrement::MultiplesOf25,
3618 );
3619 let expected_max_minus_1 = {
3620 let mut expected_max_minus_1 = Decimal::from(25u32);
3621 expected_max_minus_1.multiply_pow10(i16::MAX - 1);
3622 expected_max_minus_1
3623 };
3624 assert_eq!(expected_max_minus_1, dec);
3625
3626 let mut dec = Decimal::from(9u32);
3627 dec.multiply_pow10(i16::MAX);
3628 dec.round_with_mode_and_increment(
3629 i16::MAX,
3630 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3631 RoundingIncrement::MultiplesOf25,
3632 );
3633 let expected_max = {
3634 let mut expected_max = Decimal::from(0u32);
3635 expected_max.multiply_pow10(i16::MAX);
3636 expected_max
3637 };
3638 assert_eq!(expected_max, dec);
3639
3640 let mut dec = Decimal::from_str("0").unwrap();
3641 dec.round_with_mode_and_increment(
3642 0,
3643 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3644 RoundingIncrement::MultiplesOf2,
3645 );
3646 assert_eq!("0", dec.to_string());
3647
3648 let mut dec = Decimal::from_str("0").unwrap();
3649 dec.round_with_mode_and_increment(
3650 0,
3651 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3652 RoundingIncrement::MultiplesOf5,
3653 );
3654 assert_eq!("0", dec.to_string());
3655
3656 let mut dec = Decimal::from_str("0").unwrap();
3657 dec.round_with_mode_and_increment(
3658 0,
3659 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3660 RoundingIncrement::MultiplesOf25,
3661 );
3662 assert_eq!("0", dec.to_string());
3663
3664 let mut dec = Decimal::from_str("0.1").unwrap();
3665 dec.round_with_mode_and_increment(
3666 0,
3667 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3668 RoundingIncrement::MultiplesOf2,
3669 );
3670 assert_eq!("2", dec.to_string());
3671
3672 let mut dec = Decimal::from_str("0.1").unwrap();
3673 dec.round_with_mode_and_increment(
3674 0,
3675 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3676 RoundingIncrement::MultiplesOf5,
3677 );
3678 assert_eq!("5", dec.to_string());
3679
3680 let mut dec = Decimal::from_str("0.1").unwrap();
3681 dec.round_with_mode_and_increment(
3682 0,
3683 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3684 RoundingIncrement::MultiplesOf25,
3685 );
3686 assert_eq!("25", dec.to_string());
3687
3688 let mut dec = Decimal::from_str("1").unwrap();
3689 dec.round_with_mode_and_increment(
3690 0,
3691 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3692 RoundingIncrement::MultiplesOf2,
3693 );
3694 assert_eq!("2", dec.to_string());
3695
3696 let mut dec = Decimal::from_str("1").unwrap();
3697 dec.round_with_mode_and_increment(
3698 0,
3699 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3700 RoundingIncrement::MultiplesOf5,
3701 );
3702 assert_eq!("5", dec.to_string());
3703
3704 let mut dec = Decimal::from_str("1").unwrap();
3705 dec.round_with_mode_and_increment(
3706 0,
3707 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3708 RoundingIncrement::MultiplesOf25,
3709 );
3710 assert_eq!("25", dec.to_string());
3711
3712 let mut dec = Decimal::from_str("2").unwrap();
3713 dec.round_with_mode_and_increment(
3714 0,
3715 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3716 RoundingIncrement::MultiplesOf2,
3717 );
3718 assert_eq!("2", dec.to_string());
3719
3720 let mut dec = Decimal::from_str("2").unwrap();
3721 dec.round_with_mode_and_increment(
3722 0,
3723 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3724 RoundingIncrement::MultiplesOf5,
3725 );
3726 assert_eq!("5", dec.to_string());
3727
3728 let mut dec = Decimal::from_str("2.1").unwrap();
3729 dec.round_with_mode_and_increment(
3730 0,
3731 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3732 RoundingIncrement::MultiplesOf2,
3733 );
3734 assert_eq!("4", dec.to_string());
3735
3736 let mut dec = Decimal::from_str("2.1").unwrap();
3737 dec.round_with_mode_and_increment(
3738 0,
3739 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3740 RoundingIncrement::MultiplesOf5,
3741 );
3742 assert_eq!("5", dec.to_string());
3743
3744 let mut dec = Decimal::from_str("4").unwrap();
3745 dec.round_with_mode_and_increment(
3746 0,
3747 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3748 RoundingIncrement::MultiplesOf2,
3749 );
3750 assert_eq!("4", dec.to_string());
3751
3752 let mut dec = Decimal::from_str("4").unwrap();
3753 dec.round_with_mode_and_increment(
3754 0,
3755 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3756 RoundingIncrement::MultiplesOf5,
3757 );
3758 assert_eq!("5", dec.to_string());
3759
3760 let mut dec = Decimal::from_str("4.1").unwrap();
3761 dec.round_with_mode_and_increment(
3762 0,
3763 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3764 RoundingIncrement::MultiplesOf2,
3765 );
3766 assert_eq!("6", dec.to_string());
3767
3768 let mut dec = Decimal::from_str("4.1").unwrap();
3769 dec.round_with_mode_and_increment(
3770 0,
3771 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3772 RoundingIncrement::MultiplesOf5,
3773 );
3774 assert_eq!("5", dec.to_string());
3775
3776 let mut dec = Decimal::from_str("5").unwrap();
3777 dec.round_with_mode_and_increment(
3778 0,
3779 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3780 RoundingIncrement::MultiplesOf2,
3781 );
3782 assert_eq!("6", dec.to_string());
3783
3784 let mut dec = Decimal::from_str("5").unwrap();
3785 dec.round_with_mode_and_increment(
3786 0,
3787 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3788 RoundingIncrement::MultiplesOf5,
3789 );
3790 assert_eq!("5", dec.to_string());
3791
3792 let mut dec = Decimal::from_str("5.1").unwrap();
3793 dec.round_with_mode_and_increment(
3794 0,
3795 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3796 RoundingIncrement::MultiplesOf2,
3797 );
3798 assert_eq!("6", dec.to_string());
3799
3800 let mut dec = Decimal::from_str("5.1").unwrap();
3801 dec.round_with_mode_and_increment(
3802 0,
3803 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3804 RoundingIncrement::MultiplesOf5,
3805 );
3806 assert_eq!("10", dec.to_string());
3807
3808 let mut dec = Decimal::from_str("6").unwrap();
3809 dec.round_with_mode_and_increment(
3810 0,
3811 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3812 RoundingIncrement::MultiplesOf2,
3813 );
3814 assert_eq!("6", dec.to_string());
3815
3816 let mut dec = Decimal::from_str("6").unwrap();
3817 dec.round_with_mode_and_increment(
3818 0,
3819 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3820 RoundingIncrement::MultiplesOf5,
3821 );
3822 assert_eq!("10", dec.to_string());
3823
3824 let mut dec = Decimal::from_str("0.50").unwrap();
3825 dec.round_with_mode_and_increment(
3826 -2,
3827 SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
3828 RoundingIncrement::MultiplesOf25,
3829 );
3830 assert_eq!("0.50", dec.to_string());
3831
3832 let mut dec = Decimal::from_str("1.1025").unwrap();
3833 dec.round_with_mode_and_increment(
3834 -3,
3835 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
3836 RoundingIncrement::MultiplesOf5,
3837 );
3838 assert_eq!("1.100", dec.to_string());
3839
3840 let mut dec = Decimal::from_str("1.10125").unwrap();
3841 dec.round_with_mode_and_increment(
3842 -4,
3843 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
3844 RoundingIncrement::MultiplesOf25,
3845 );
3846 assert_eq!("1.1025", dec.to_string());
3847
3848 let mut dec = Decimal::from_str("-1.25").unwrap();
3849 dec.round_with_mode_and_increment(
3850 -1,
3851 SignedRoundingMode::HalfCeil,
3852 RoundingIncrement::MultiplesOf5,
3853 );
3854 assert_eq!("-1.0", dec.to_string());
3855
3856 let mut dec = Decimal::from_str("-1.251").unwrap();
3857 dec.round_with_mode_and_increment(
3858 -1,
3859 SignedRoundingMode::HalfCeil,
3860 RoundingIncrement::MultiplesOf5,
3861 );
3862 assert_eq!("-1.5", dec.to_string());
3863
3864 let mut dec = Decimal::from_str("-1.125").unwrap();
3865 dec.round_with_mode_and_increment(
3866 -2,
3867 SignedRoundingMode::HalfFloor,
3868 RoundingIncrement::MultiplesOf25,
3869 );
3870 assert_eq!("-1.25", dec.to_string());
3871
3872 let mut dec = Decimal::from_str("2.71").unwrap();
3873 dec.round_with_mode_and_increment(
3874 -2,
3875 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3876 RoundingIncrement::MultiplesOf2,
3877 );
3878 assert_eq!("2.72", dec.to_string());
3879
3880 let mut dec = Decimal::from_str("2.73").unwrap();
3881 dec.round_with_mode_and_increment(
3882 -2,
3883 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3884 RoundingIncrement::MultiplesOf2,
3885 );
3886 assert_eq!("2.72", dec.to_string());
3887
3888 let mut dec = Decimal::from_str("2.75").unwrap();
3889 dec.round_with_mode_and_increment(
3890 -2,
3891 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3892 RoundingIncrement::MultiplesOf2,
3893 );
3894 assert_eq!("2.76", dec.to_string());
3895
3896 let mut dec = Decimal::from_str("2.77").unwrap();
3897 dec.round_with_mode_and_increment(
3898 -2,
3899 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3900 RoundingIncrement::MultiplesOf2,
3901 );
3902 assert_eq!("2.76", dec.to_string());
3903
3904 let mut dec = Decimal::from_str("2.79").unwrap();
3905 dec.round_with_mode_and_increment(
3906 -2,
3907 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3908 RoundingIncrement::MultiplesOf2,
3909 );
3910 assert_eq!("2.80", dec.to_string());
3911
3912 let mut dec = Decimal::from_str("2.41").unwrap();
3913 dec.round_with_mode_and_increment(
3914 -2,
3915 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3916 RoundingIncrement::MultiplesOf2,
3917 );
3918 assert_eq!("2.40", dec.to_string());
3919
3920 let mut dec = Decimal::from_str("2.43").unwrap();
3921 dec.round_with_mode_and_increment(
3922 -2,
3923 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3924 RoundingIncrement::MultiplesOf2,
3925 );
3926 assert_eq!("2.44", dec.to_string());
3927
3928 let mut dec = Decimal::from_str("2.45").unwrap();
3929 dec.round_with_mode_and_increment(
3930 -2,
3931 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3932 RoundingIncrement::MultiplesOf2,
3933 );
3934 assert_eq!("2.44", dec.to_string());
3935
3936 let mut dec = Decimal::from_str("2.47").unwrap();
3937 dec.round_with_mode_and_increment(
3938 -2,
3939 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3940 RoundingIncrement::MultiplesOf2,
3941 );
3942 assert_eq!("2.48", dec.to_string());
3943
3944 let mut dec = Decimal::from_str("2.49").unwrap();
3945 dec.round_with_mode_and_increment(
3946 -2,
3947 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3948 RoundingIncrement::MultiplesOf2,
3949 );
3950 assert_eq!("2.48", dec.to_string());
3951
3952 let mut dec = Decimal::from_str("2.725").unwrap();
3953 dec.round_with_mode_and_increment(
3954 -2,
3955 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3956 RoundingIncrement::MultiplesOf5,
3957 );
3958 assert_eq!("2.70", dec.to_string());
3959
3960 let mut dec = Decimal::from_str("2.775").unwrap();
3961 dec.round_with_mode_and_increment(
3962 -2,
3963 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3964 RoundingIncrement::MultiplesOf5,
3965 );
3966 assert_eq!("2.80", dec.to_string());
3967
3968 let mut dec = Decimal::from_str("2.875").unwrap();
3969 dec.round_with_mode_and_increment(
3970 -2,
3971 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3972 RoundingIncrement::MultiplesOf25,
3973 );
3974 assert_eq!("3.00", dec.to_string());
3975
3976 let mut dec = Decimal::from_str("2.375").unwrap();
3977 dec.round_with_mode_and_increment(
3978 -2,
3979 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3980 RoundingIncrement::MultiplesOf25,
3981 );
3982 assert_eq!("2.50", dec.to_string());
3983
3984 let mut dec = Decimal::from_str("2.125").unwrap();
3985 dec.round_with_mode_and_increment(
3986 -2,
3987 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3988 RoundingIncrement::MultiplesOf25,
3989 );
3990 assert_eq!("2.00", dec.to_string());
3991
3992 let mut dec = Decimal::from_str("2.625").unwrap();
3993 dec.round_with_mode_and_increment(
3994 -2,
3995 SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
3996 RoundingIncrement::MultiplesOf25,
3997 );
3998 assert_eq!("2.50", dec.to_string());
3999 }