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