use core::fmt;
use core::ops::{Deref, DerefMut};
use core::str::FromStr;
use crate::uint_iterator::IntIterator;
use crate::{variations::Signed, UnsignedFixedDecimal};
#[cfg(feature = "ryu")]
use crate::{FloatPrecision, LimitError};
use crate::{
IncrementLike, NoIncrement, ParseError, RoundingIncrement, Sign, SignDisplay,
SignedRoundingMode, UnsignedRoundingMode,
};
pub type SignedFixedDecimal = Signed<UnsignedFixedDecimal>;
impl SignedFixedDecimal {
pub fn new(sign: Sign, absolute: UnsignedFixedDecimal) -> Self {
SignedFixedDecimal { sign, absolute }
}
#[inline]
pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
Self::try_from_utf8(s.as_bytes())
}
pub fn try_from_utf8(input_str: &[u8]) -> Result<Self, ParseError> {
if input_str.is_empty() {
return Err(ParseError::Syntax);
}
#[allow(clippy::indexing_slicing)] let sign = match input_str[0] {
b'-' => Sign::Negative,
b'+' => Sign::Positive,
_ => Sign::None,
};
#[allow(clippy::indexing_slicing)] let no_sign_str = if sign == Sign::None {
input_str
} else {
&input_str[1..]
};
if no_sign_str.is_empty() {
return Err(ParseError::Syntax);
}
let unsigned_decimal = UnsignedFixedDecimal::try_from_no_sign_utf8(no_sign_str)?;
Ok(Self {
sign,
absolute: unsigned_decimal,
})
}
pub fn apply_sign_display(&mut self, sign_display: SignDisplay) {
use Sign::*;
match sign_display {
SignDisplay::Auto => {
if self.sign != Negative {
self.sign = None
}
}
SignDisplay::Always => {
if self.sign != Negative {
self.sign = Positive
}
}
SignDisplay::Never => self.sign = None,
SignDisplay::ExceptZero => {
if self.absolute.is_zero() {
self.sign = None
} else if self.sign != Negative {
self.sign = Positive
}
}
SignDisplay::Negative => {
if self.sign != Negative || self.absolute.is_zero() {
self.sign = None
}
}
}
}
pub fn with_sign_display(mut self, sign_display: SignDisplay) -> Self {
self.apply_sign_display(sign_display);
self
}
}
impl FromStr for SignedFixedDecimal {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from_str(s)
}
}
macro_rules! impl_from_signed_integer_type {
($itype:ident, $utype: ident) => {
impl From<$itype> for SignedFixedDecimal {
fn from(value: $itype) -> Self {
let int_iterator: IntIterator<$utype> = value.into();
let sign = if int_iterator.is_negative {
Sign::Negative
} else {
Sign::None
};
let value = UnsignedFixedDecimal::from_ascending(int_iterator)
.expect("All built-in integer types should fit");
SignedFixedDecimal {
sign,
absolute: value,
}
}
}
};
}
impl_from_signed_integer_type!(isize, usize);
impl_from_signed_integer_type!(i128, u128);
impl_from_signed_integer_type!(i64, u64);
impl_from_signed_integer_type!(i32, u32);
impl_from_signed_integer_type!(i16, u16);
impl_from_signed_integer_type!(i8, u8);
macro_rules! impl_from_unsigned_integer_type {
($utype: ident) => {
impl From<$utype> for SignedFixedDecimal {
fn from(value: $utype) -> Self {
let int_iterator: IntIterator<$utype> = value.into();
Self {
sign: Sign::None,
absolute: UnsignedFixedDecimal::from_ascending(int_iterator)
.expect("All built-in integer types should fit"),
}
}
}
};
}
impl_from_unsigned_integer_type!(usize);
impl_from_unsigned_integer_type!(u128);
impl_from_unsigned_integer_type!(u64);
impl_from_unsigned_integer_type!(u32);
impl_from_unsigned_integer_type!(u16);
impl_from_unsigned_integer_type!(u8);
#[cfg(feature = "ryu")]
impl SignedFixedDecimal {
pub fn try_from_f64(float: f64, precision: FloatPrecision) -> Result<Self, LimitError> {
match float.is_sign_negative() {
true => Ok(SignedFixedDecimal {
sign: Sign::Negative,
absolute: UnsignedFixedDecimal::try_from_f64(-float, precision)?,
}),
false => Ok(SignedFixedDecimal {
sign: Sign::None,
absolute: UnsignedFixedDecimal::try_from_f64(float, precision)?,
}),
}
}
}
impl Deref for SignedFixedDecimal {
type Target = UnsignedFixedDecimal;
fn deref(&self) -> &Self::Target {
&self.absolute
}
}
impl DerefMut for SignedFixedDecimal {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.absolute
}
}
impl SignedFixedDecimal {
pub fn round(&mut self, position: i16) {
self.half_even_to_increment_internal(position, NoIncrement)
}
pub fn rounded(mut self, position: i16) -> Self {
self.round(position);
self
}
#[inline(never)]
pub fn ceil(&mut self, position: i16) {
self.ceil_to_increment_internal(position, NoIncrement);
}
pub fn ceiled(mut self, position: i16) -> Self {
self.ceil(position);
self
}
#[inline(never)]
pub fn expand(&mut self, position: i16) {
self.expand_to_increment_internal(position, NoIncrement)
}
pub fn expanded(mut self, position: i16) -> Self {
self.expand(position);
self
}
#[inline(never)]
pub fn floor(&mut self, position: i16) {
self.floor_to_increment_internal(position, NoIncrement);
}
pub fn floored(mut self, position: i16) -> Self {
self.floor(position);
self
}
#[inline(never)]
pub fn trunc(&mut self, position: i16) {
self.trunc_to_increment_internal(position, NoIncrement)
}
pub fn trunced(mut self, position: i16) -> Self {
self.trunc(position);
self
}
pub fn round_with_mode(&mut self, position: i16, mode: SignedRoundingMode) {
match mode {
SignedRoundingMode::Ceil => self.ceil_to_increment_internal(position, NoIncrement),
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand) => {
self.expand_to_increment_internal(position, NoIncrement)
}
SignedRoundingMode::Floor => self.floor_to_increment_internal(position, NoIncrement),
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc) => {
self.trunc_to_increment_internal(position, NoIncrement)
}
SignedRoundingMode::HalfCeil => {
self.half_ceil_to_increment_internal(position, NoIncrement)
}
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand) => {
self.half_expand_to_increment_internal(position, NoIncrement)
}
SignedRoundingMode::HalfFloor => {
self.half_floor_to_increment_internal(position, NoIncrement)
}
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc) => {
self.half_trunc_to_increment_internal(position, NoIncrement)
}
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven) => {
self.half_even_to_increment_internal(position, NoIncrement)
}
}
}
pub fn rounded_with_mode(mut self, position: i16, mode: SignedRoundingMode) -> Self {
self.round_with_mode(position, mode);
self
}
pub fn round_with_mode_and_increment(
&mut self,
position: i16,
mode: SignedRoundingMode,
increment: RoundingIncrement,
) {
match mode {
SignedRoundingMode::Ceil => self.ceil_to_increment_internal(position, increment),
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand) => {
self.expand_to_increment_internal(position, increment)
}
SignedRoundingMode::Floor => self.floor_to_increment_internal(position, increment),
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc) => {
self.trunc_to_increment_internal(position, increment)
}
SignedRoundingMode::HalfCeil => {
self.half_ceil_to_increment_internal(position, increment)
}
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand) => {
self.half_expand_to_increment_internal(position, increment)
}
SignedRoundingMode::HalfFloor => {
self.half_floor_to_increment_internal(position, increment)
}
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc) => {
self.half_trunc_to_increment_internal(position, increment)
}
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven) => {
self.half_even_to_increment_internal(position, increment)
}
}
}
pub fn rounded_with_mode_and_increment(
mut self,
position: i16,
mode: SignedRoundingMode,
increment: RoundingIncrement,
) -> Self {
self.round_with_mode_and_increment(position, mode, increment);
self
}
fn ceil_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
if self.sign == Sign::Negative {
self.trunc_to_increment_internal(position, increment);
return;
}
self.expand_to_increment_internal(position, increment);
}
fn half_ceil_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
if self.sign == Sign::Negative {
self.half_trunc_to_increment_internal(position, increment);
return;
}
self.half_expand_to_increment_internal(position, increment);
}
fn floor_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
if self.sign == Sign::Negative {
self.expand_to_increment_internal(position, increment);
return;
}
self.trunc_to_increment_internal(position, increment);
}
fn half_floor_to_increment_internal<R: IncrementLike>(&mut self, position: i16, increment: R) {
if self.sign == Sign::Negative {
self.half_expand_to_increment_internal(position, increment);
return;
}
self.half_trunc_to_increment_internal(position, increment);
}
}
impl writeable::Writeable for SignedFixedDecimal {
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
match self.sign {
Sign::Negative => sink.write_char('-')?,
Sign::Positive => sink.write_char('+')?,
Sign::None => (),
}
for m in self.absolute.magnitude_range().rev() {
if m == -1 {
sink.write_char('.')?;
}
let d = self.absolute.digit_at(m);
sink.write_char((b'0' + d) as char)?;
}
Ok(())
}
fn writeable_length_hint(&self) -> writeable::LengthHint {
self.absolute.writeable_length_hint() + (self.sign != Sign::None) as usize
}
}
writeable::impl_display_with_writeable!(SignedFixedDecimal);
#[test]
fn test_basic() {
#[derive(Debug)]
struct TestCase {
pub input: isize,
pub delta: i16,
pub expected: &'static str,
}
let cases = [
TestCase {
input: 51423,
delta: 0,
expected: "51423",
},
TestCase {
input: 51423,
delta: -2,
expected: "514.23",
},
TestCase {
input: 51423,
delta: -5,
expected: "0.51423",
},
TestCase {
input: 51423,
delta: -8,
expected: "0.00051423",
},
TestCase {
input: 51423,
delta: 3,
expected: "51423000",
},
TestCase {
input: 0,
delta: 0,
expected: "0",
},
TestCase {
input: 0,
delta: -2,
expected: "0.00",
},
TestCase {
input: 0,
delta: 3,
expected: "0000",
},
TestCase {
input: 500,
delta: 0,
expected: "500",
},
TestCase {
input: 500,
delta: -1,
expected: "50.0",
},
TestCase {
input: 500,
delta: -2,
expected: "5.00",
},
TestCase {
input: 500,
delta: -3,
expected: "0.500",
},
TestCase {
input: 500,
delta: -4,
expected: "0.0500",
},
TestCase {
input: 500,
delta: 3,
expected: "500000",
},
TestCase {
input: -123,
delta: 0,
expected: "-123",
},
TestCase {
input: -123,
delta: -2,
expected: "-1.23",
},
TestCase {
input: -123,
delta: -5,
expected: "-0.00123",
},
TestCase {
input: -123,
delta: 3,
expected: "-123000",
},
];
for cas in &cases {
let mut dec: SignedFixedDecimal = cas.input.into();
dec.absolute.multiply_pow10(cas.delta);
writeable::assert_writeable_eq!(dec, cas.expected, "{:?}", cas);
}
}
#[test]
fn test_from_str() {
#[derive(Debug)]
struct TestCase {
pub input_str: &'static str,
pub output_str: Option<&'static str>,
pub magnitudes: [i16; 4],
}
let cases = [
TestCase {
input_str: "-00123400",
output_str: None,
magnitudes: [7, 5, 2, 0],
},
TestCase {
input_str: "+00123400",
output_str: None,
magnitudes: [7, 5, 2, 0],
},
TestCase {
input_str: "0.0123400",
output_str: None,
magnitudes: [0, -2, -5, -7],
},
TestCase {
input_str: "-00.123400",
output_str: None,
magnitudes: [1, -1, -4, -6],
},
TestCase {
input_str: "0012.3400",
output_str: None,
magnitudes: [3, 1, -2, -4],
},
TestCase {
input_str: "-0012340.0",
output_str: None,
magnitudes: [6, 4, 1, -1],
},
TestCase {
input_str: "1234",
output_str: None,
magnitudes: [3, 3, 0, 0],
},
TestCase {
input_str: "0.000000001",
output_str: None,
magnitudes: [0, -9, -9, -9],
},
TestCase {
input_str: "0.0000000010",
output_str: None,
magnitudes: [0, -9, -9, -10],
},
TestCase {
input_str: "1000000",
output_str: None,
magnitudes: [6, 6, 6, 0],
},
TestCase {
input_str: "10000001",
output_str: None,
magnitudes: [7, 7, 0, 0],
},
TestCase {
input_str: "123",
output_str: None,
magnitudes: [2, 2, 0, 0],
},
TestCase {
input_str: "922337203685477580898230948203840239384.9823094820384023938423424",
output_str: None,
magnitudes: [38, 38, -25, -25],
},
TestCase {
input_str: "009223372000.003685477580898230948203840239384000",
output_str: None,
magnitudes: [11, 9, -33, -36],
},
TestCase {
input_str: "-009223372000.003685477580898230948203840239384000",
output_str: None,
magnitudes: [11, 9, -33, -36],
},
TestCase {
input_str: "0",
output_str: None,
magnitudes: [0, 0, 0, 0],
},
TestCase {
input_str: "-0",
output_str: None,
magnitudes: [0, 0, 0, 0],
},
TestCase {
input_str: "+0",
output_str: None,
magnitudes: [0, 0, 0, 0],
},
TestCase {
input_str: "000",
output_str: None,
magnitudes: [2, 0, 0, 0],
},
TestCase {
input_str: "-00.0",
output_str: None,
magnitudes: [1, 0, 0, -1],
},
TestCase {
input_str: ".0123400",
output_str: Some("0.0123400"),
magnitudes: [0, -2, -5, -7],
},
TestCase {
input_str: ".000000001",
output_str: Some("0.000000001"),
magnitudes: [0, -9, -9, -9],
},
TestCase {
input_str: "-.123400",
output_str: Some("-0.123400"),
magnitudes: [0, -1, -4, -6],
},
];
for cas in &cases {
let fd = SignedFixedDecimal::from_str(cas.input_str).unwrap();
assert_eq!(
fd.absolute.magnitude_range(),
cas.magnitudes[3]..=cas.magnitudes[0],
"{cas:?}"
);
assert_eq!(
fd.absolute.nonzero_magnitude_start(),
cas.magnitudes[1],
"{cas:?}"
);
assert_eq!(
fd.absolute.nonzero_magnitude_end(),
cas.magnitudes[2],
"{cas:?}"
);
let input_str_roundtrip = fd.to_string();
let output_str = cas.output_str.unwrap_or(cas.input_str);
assert_eq!(output_str, input_str_roundtrip, "{cas:?}");
}
}
#[test]
fn test_from_str_scientific() {
#[derive(Debug)]
struct TestCase {
pub input_str: &'static str,
pub output: &'static str,
}
let cases = [
TestCase {
input_str: "-5.4e10",
output: "-54000000000",
},
TestCase {
input_str: "5.4e-2",
output: "0.054",
},
TestCase {
input_str: "54.1e-2",
output: "0.541",
},
TestCase {
input_str: "-541e-2",
output: "-5.41",
},
TestCase {
input_str: "0.009E10",
output: "90000000",
},
TestCase {
input_str: "-9000E-10",
output: "-0.0000009",
},
];
for cas in &cases {
let input_str_roundtrip = SignedFixedDecimal::from_str(cas.input_str)
.unwrap()
.to_string();
assert_eq!(cas.output, input_str_roundtrip);
}
}
#[test]
fn test_isize_limits() {
for num in &[isize::MAX, isize::MIN] {
let dec: SignedFixedDecimal = (*num).into();
let dec_str = dec.to_string();
assert_eq!(num.to_string(), dec_str);
assert_eq!(dec, SignedFixedDecimal::from_str(&dec_str).unwrap());
writeable::assert_writeable_eq!(dec, dec_str);
}
}
#[test]
fn test_ui128_limits() {
for num in &[i128::MAX, i128::MIN] {
let dec: SignedFixedDecimal = (*num).into();
let dec_str = dec.to_string();
assert_eq!(num.to_string(), dec_str);
assert_eq!(dec, SignedFixedDecimal::from_str(&dec_str).unwrap());
writeable::assert_writeable_eq!(dec, dec_str);
}
for num in &[u128::MAX, u128::MIN] {
let dec: SignedFixedDecimal = (*num).into();
let dec_str = dec.to_string();
assert_eq!(num.to_string(), dec_str);
assert_eq!(dec, SignedFixedDecimal::from_str(&dec_str).unwrap());
writeable::assert_writeable_eq!(dec, dec_str);
}
}
#[test]
fn test_zero_str_bounds() {
#[derive(Debug)]
struct TestCase {
pub zeros_before_dot: usize,
pub zeros_after_dot: usize,
pub expected_err: Option<ParseError>,
}
let cases = [
TestCase {
zeros_before_dot: i16::MAX as usize + 1,
zeros_after_dot: 0,
expected_err: None,
},
TestCase {
zeros_before_dot: i16::MAX as usize,
zeros_after_dot: 0,
expected_err: None,
},
TestCase {
zeros_before_dot: i16::MAX as usize + 2,
zeros_after_dot: 0,
expected_err: Some(ParseError::Limit),
},
TestCase {
zeros_before_dot: 0,
zeros_after_dot: i16::MAX as usize + 2,
expected_err: Some(ParseError::Limit),
},
TestCase {
zeros_before_dot: i16::MAX as usize + 1,
zeros_after_dot: i16::MAX as usize + 1,
expected_err: None,
},
TestCase {
zeros_before_dot: i16::MAX as usize + 2,
zeros_after_dot: i16::MAX as usize + 1,
expected_err: Some(ParseError::Limit),
},
TestCase {
zeros_before_dot: i16::MAX as usize + 1,
zeros_after_dot: i16::MAX as usize + 2,
expected_err: Some(ParseError::Limit),
},
TestCase {
zeros_before_dot: i16::MAX as usize,
zeros_after_dot: i16::MAX as usize + 2,
expected_err: Some(ParseError::Limit),
},
TestCase {
zeros_before_dot: i16::MAX as usize,
zeros_after_dot: i16::MAX as usize,
expected_err: None,
},
TestCase {
zeros_before_dot: i16::MAX as usize + 1,
zeros_after_dot: i16::MAX as usize,
expected_err: None,
},
];
for cas in &cases {
let mut input_str = format!("{:0fill$}", 0, fill = cas.zeros_before_dot);
if cas.zeros_after_dot > 0 {
input_str.push('.');
input_str.push_str(&format!("{:0fill$}", 0, fill = cas.zeros_after_dot));
}
match SignedFixedDecimal::from_str(&input_str) {
Ok(dec) => {
assert_eq!(cas.expected_err, None, "{cas:?}");
assert_eq!(input_str, dec.to_string(), "{cas:?}");
}
Err(err) => {
assert_eq!(cas.expected_err, Some(err), "{cas:?}");
}
}
}
}
#[test]
fn test_syntax_error() {
#[derive(Debug)]
struct TestCase {
pub input_str: &'static str,
pub expected_err: Option<ParseError>,
}
let cases = [
TestCase {
input_str: "-12a34",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "0.0123√400",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "0.012.3400",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "-0-0123400",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "0-0123400",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "-0.00123400",
expected_err: None,
},
TestCase {
input_str: "00123400.",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "00123400.0",
expected_err: None,
},
TestCase {
input_str: "123_456",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "-",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "+",
expected_err: Some(ParseError::Syntax),
},
TestCase {
input_str: "-1",
expected_err: None,
},
];
for cas in &cases {
match SignedFixedDecimal::from_str(cas.input_str) {
Ok(dec) => {
assert_eq!(cas.expected_err, None, "{cas:?}");
assert_eq!(cas.input_str, dec.to_string(), "{cas:?}");
}
Err(err) => {
assert_eq!(cas.expected_err, Some(err), "{cas:?}");
}
}
}
}
#[test]
fn test_pad() {
let mut dec = SignedFixedDecimal::from_str("-0.42").unwrap();
assert_eq!("-0.42", dec.to_string());
dec.absolute.pad_start(1);
assert_eq!("-0.42", dec.to_string());
dec.absolute.pad_start(4);
assert_eq!("-0000.42", dec.to_string());
dec.absolute.pad_start(2);
assert_eq!("-00.42", dec.to_string());
}
#[test]
fn test_sign_display() {
use SignDisplay::*;
let positive_nonzero = SignedFixedDecimal::from(163u32);
let negative_nonzero = SignedFixedDecimal::from(-163);
let positive_zero = SignedFixedDecimal::from(0u32);
let negative_zero = SignedFixedDecimal::from(0u32).with_sign(Sign::Negative);
assert_eq!(
"163",
positive_nonzero.clone().with_sign_display(Auto).to_string()
);
assert_eq!(
"-163",
negative_nonzero.clone().with_sign_display(Auto).to_string()
);
assert_eq!(
"0",
positive_zero.clone().with_sign_display(Auto).to_string()
);
assert_eq!(
"-0",
negative_zero.clone().with_sign_display(Auto).to_string()
);
assert_eq!(
"+163",
positive_nonzero
.clone()
.with_sign_display(Always)
.to_string()
);
assert_eq!(
"-163",
negative_nonzero
.clone()
.with_sign_display(Always)
.to_string()
);
assert_eq!(
"+0",
positive_zero.clone().with_sign_display(Always).to_string()
);
assert_eq!(
"-0",
negative_zero.clone().with_sign_display(Always).to_string()
);
assert_eq!(
"163",
positive_nonzero
.clone()
.with_sign_display(Never)
.to_string()
);
assert_eq!(
"163",
negative_nonzero
.clone()
.with_sign_display(Never)
.to_string()
);
assert_eq!(
"0",
positive_zero.clone().with_sign_display(Never).to_string()
);
assert_eq!(
"0",
negative_zero.clone().with_sign_display(Never).to_string()
);
assert_eq!(
"+163",
positive_nonzero
.clone()
.with_sign_display(ExceptZero)
.to_string()
);
assert_eq!(
"-163",
negative_nonzero
.clone()
.with_sign_display(ExceptZero)
.to_string()
);
assert_eq!(
"0",
positive_zero
.clone()
.with_sign_display(ExceptZero)
.to_string()
);
assert_eq!(
"0",
negative_zero
.clone()
.with_sign_display(ExceptZero)
.to_string()
);
assert_eq!(
"163",
positive_nonzero.with_sign_display(Negative).to_string()
);
assert_eq!(
"-163",
negative_nonzero.with_sign_display(Negative).to_string()
);
assert_eq!("0", positive_zero.with_sign_display(Negative).to_string());
assert_eq!("0", negative_zero.with_sign_display(Negative).to_string());
}
#[test]
fn test_set_max_position() {
let mut dec = SignedFixedDecimal::from(1000u32);
assert_eq!("1000", dec.to_string());
dec.absolute.set_max_position(2);
assert_eq!("00", dec.to_string());
dec.absolute.set_max_position(0);
assert_eq!("0", dec.to_string());
dec.absolute.set_max_position(3);
assert_eq!("000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.456").unwrap();
assert_eq!("0.456", dec.to_string());
dec.absolute.set_max_position(0);
assert_eq!("0.456", dec.to_string());
dec.absolute.set_max_position(-1);
assert_eq!("0.056", dec.to_string());
dec.absolute.set_max_position(-2);
assert_eq!("0.006", dec.to_string());
dec.absolute.set_max_position(-3);
assert_eq!("0.000", dec.to_string());
dec.absolute.set_max_position(-4);
assert_eq!("0.0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("100.01").unwrap();
dec.absolute.set_max_position(1);
assert_eq!("0.01", dec.to_string());
}
#[test]
fn test_pad_start_bounds() {
let mut dec = SignedFixedDecimal::from_str("299792.458").unwrap();
let max_integer_digits = i16::MAX as usize + 1;
dec.absolute.pad_start(i16::MAX - 1);
assert_eq!(
max_integer_digits - 2,
dec.to_string().split_once('.').unwrap().0.len()
);
dec.absolute.pad_start(i16::MAX);
assert_eq!(
max_integer_digits - 1,
dec.to_string().split_once('.').unwrap().0.len()
);
}
#[test]
fn test_pad_end_bounds() {
let mut dec = SignedFixedDecimal::from_str("299792.458").unwrap();
let max_fractional_digits = -(i16::MIN as isize) as usize;
dec.absolute.pad_end(i16::MIN + 1);
assert_eq!(
max_fractional_digits - 1,
dec.to_string().split_once('.').unwrap().1.len()
);
dec.absolute.pad_end(i16::MIN);
assert_eq!(
max_fractional_digits,
dec.to_string().split_once('.').unwrap().1.len()
);
}
#[test]
fn test_rounding() {
pub(crate) use std::str::FromStr;
let mut dec = SignedFixedDecimal::from_str("3.234").unwrap();
dec.ceil(0);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.222").unwrap();
dec.ceil(-1);
assert_eq!("2.3", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("22.222").unwrap();
dec.ceil(-2);
assert_eq!("22.23", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.ceil(-2);
assert_eq!("100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.ceil(-5);
assert_eq!("99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.ceil(-5);
assert_eq!("-99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.ceil(-2);
assert_eq!("-99.99", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.ceil(4);
assert_eq!("10000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.ceil(4);
assert_eq!("-0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.ceil(-1);
assert_eq!("0.1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-0.009").unwrap();
dec.ceil(-1);
assert_eq!("-0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.234").unwrap();
dec.round_with_mode(0, SignedRoundingMode::HalfCeil);
assert_eq!("3", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.534").unwrap();
dec.round_with_mode(0, SignedRoundingMode::HalfCeil);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.934").unwrap();
dec.round_with_mode(0, SignedRoundingMode::HalfCeil);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.222").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
assert_eq!("2.2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.44").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
assert_eq!("2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.45").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
assert_eq!("2.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-2.44").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
assert_eq!("-2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-2.45").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
assert_eq!("-2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("22.222").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::HalfCeil);
assert_eq!("22.22", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::HalfCeil);
assert_eq!("100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(-5, SignedRoundingMode::HalfCeil);
assert_eq!("99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(-5, SignedRoundingMode::HalfCeil);
assert_eq!("-99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::HalfCeil);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(4, SignedRoundingMode::HalfCeil);
assert_eq!("0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(4, SignedRoundingMode::HalfCeil);
assert_eq!("-0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-0.009").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfCeil);
assert_eq!("-0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.234").unwrap();
dec.floor(0);
assert_eq!("3", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.222").unwrap();
dec.floor(-1);
assert_eq!("2.2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.floor(-2);
assert_eq!("99.99", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.floor(-10);
assert_eq!("99.9990000000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.floor(-10);
assert_eq!("-99.9990000000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.floor(10);
assert_eq!("0000000000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.floor(10);
assert_eq!("-10000000000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.234").unwrap();
dec.round_with_mode(0, SignedRoundingMode::HalfFloor);
assert_eq!("3", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.534").unwrap();
dec.round_with_mode(0, SignedRoundingMode::HalfFloor);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.934").unwrap();
dec.round_with_mode(0, SignedRoundingMode::HalfFloor);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.222").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
assert_eq!("2.2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.44").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
assert_eq!("2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.45").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
assert_eq!("2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-2.44").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
assert_eq!("-2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-2.45").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
assert_eq!("-2.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("22.222").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::HalfFloor);
assert_eq!("22.22", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::HalfFloor);
assert_eq!("100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(-5, SignedRoundingMode::HalfFloor);
assert_eq!("99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(-5, SignedRoundingMode::HalfFloor);
assert_eq!("-99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::HalfFloor);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(4, SignedRoundingMode::HalfFloor);
assert_eq!("0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(4, SignedRoundingMode::HalfFloor);
assert_eq!("-0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-0.009").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::HalfFloor);
assert_eq!("-0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.trunc(-5);
assert_eq!("4235.97000", dec.to_string());
dec.trunc(-1);
assert_eq!("4235.9", dec.to_string());
dec.trunc(0);
assert_eq!("4235", dec.to_string());
dec.trunc(1);
assert_eq!("4230", dec.to_string());
dec.trunc(5);
assert_eq!("00000", dec.to_string());
dec.trunc(2);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.trunc(-2);
assert_eq!("-99.99", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.trunc(-1);
assert_eq!("1234.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.trunc(-1);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
assert_eq!("4235.97000", dec.clone().trunced(-5).to_string());
assert_eq!("4230", dec.clone().trunced(1).to_string());
assert_eq!("4200", dec.clone().trunced(2).to_string());
assert_eq!("00000", dec.trunced(5).to_string());
let mut dec = SignedFixedDecimal::from_str("3.234").unwrap();
dec.expand(0);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.222").unwrap();
dec.expand(-1);
assert_eq!("2.3", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("22.222").unwrap();
dec.expand(-2);
assert_eq!("22.23", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.expand(-2);
assert_eq!("100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.expand(-5);
assert_eq!("99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.expand(-5);
assert_eq!("-99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.expand(-2);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.expand(4);
assert_eq!("10000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.expand(4);
assert_eq!("-10000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.expand(-1);
assert_eq!("0.1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-0.009").unwrap();
dec.expand(-1);
assert_eq!("-0.1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.954").unwrap();
dec.expand(0);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.234").unwrap();
dec.round_with_mode(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("3", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.534").unwrap();
dec.round_with_mode(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("3.934").unwrap();
dec.round_with_mode(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.222").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("2.2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.44").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.45").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("2.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-2.44").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("-2.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-2.45").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("-2.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("22.222").unwrap();
dec.round_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("22.22", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(
-5,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(
-5,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("-99.99900", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("99.999").unwrap();
dec.round_with_mode(
4,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode(
4,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("-0000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-0.009").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("-0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.108").unwrap();
dec.round_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
);
assert_eq!("1.11", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.108").unwrap();
dec.expand(-2);
assert_eq!("1.11", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.108").unwrap();
dec.trunc(-2);
assert_eq!("1.10", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.78536913177").unwrap();
dec.round_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
);
assert_eq!("2.79", dec.to_string());
}
#[test]
fn test_concatenate() {
#[derive(Debug)]
struct TestCase {
pub input_1: &'static str,
pub input_2: &'static str,
pub expected: Option<&'static str>,
}
let cases = [
TestCase {
input_1: "123",
input_2: "0.456",
expected: Some("123.456"),
},
TestCase {
input_1: "0.456",
input_2: "123",
expected: None,
},
TestCase {
input_1: "123",
input_2: "0.0456",
expected: Some("123.0456"),
},
TestCase {
input_1: "0.0456",
input_2: "123",
expected: None,
},
TestCase {
input_1: "100",
input_2: "0.456",
expected: Some("100.456"),
},
TestCase {
input_1: "0.456",
input_2: "100",
expected: None,
},
TestCase {
input_1: "100",
input_2: "0.001",
expected: Some("100.001"),
},
TestCase {
input_1: "0.001",
input_2: "100",
expected: None,
},
TestCase {
input_1: "123000",
input_2: "456",
expected: Some("123456"),
},
TestCase {
input_1: "456",
input_2: "123000",
expected: None,
},
TestCase {
input_1: "5",
input_2: "5",
expected: None,
},
TestCase {
input_1: "120",
input_2: "25",
expected: None,
},
TestCase {
input_1: "1.1",
input_2: "0.2",
expected: None,
},
TestCase {
input_1: "0",
input_2: "222",
expected: Some("222"),
},
TestCase {
input_1: "222",
input_2: "0",
expected: Some("222"),
},
TestCase {
input_1: "0",
input_2: "0",
expected: Some("0"),
},
TestCase {
input_1: "000",
input_2: "0",
expected: Some("000"),
},
TestCase {
input_1: "0.00",
input_2: "0",
expected: Some("0.00"),
},
];
for cas in &cases {
let fd1 = SignedFixedDecimal::from_str(cas.input_1).unwrap();
let fd2 = SignedFixedDecimal::from_str(cas.input_2).unwrap();
match fd1.absolute.concatenated_end(fd2.absolute) {
Ok(fd) => {
assert_eq!(cas.expected, Some(fd.to_string().as_str()), "{cas:?}");
}
Err(_) => {
assert!(cas.expected.is_none(), "{cas:?}");
}
}
}
}
#[test]
fn test_rounding_increment() {
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.96", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4235.5", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4225", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("00000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-99.75", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.25", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.700", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(6u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(5u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(50u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(6u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf5,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(5u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc),
RoundingIncrement::MultiplesOf25,
);
let expected = {
let mut expected = SignedFixedDecimal::from(0u32);
expected.multiply_pow10(i16::MAX);
expected
};
assert_eq!(expected, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.98", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4236.0", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4250", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("500000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("500000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.75", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.702", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("25", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(8u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(10u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(75u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(8u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(0u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(0u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.96", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4236.0", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4225", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("00000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.700", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(6u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(10u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(75u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(6u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf5,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(5u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf25,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(0u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.98", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4236.0", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4225", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("00000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.700", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(8u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(10u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(75u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(8u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.98", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4236.0", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4250", dec.to_string());
dec.round_with_mode_and_increment(5, SignedRoundingMode::Ceil, RoundingIncrement::MultiplesOf5);
assert_eq!("500000", dec.to_string());
dec.round_with_mode_and_increment(2, SignedRoundingMode::Ceil, RoundingIncrement::MultiplesOf2);
assert_eq!("500000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-99.75", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.75", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.702", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("25", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(8u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(10u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(75u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Ceil,
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(8u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.98", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4236.0", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4225", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("00000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.700", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(8u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(10u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(75u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(8u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.96", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4235.5", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4225", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("00000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.25", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.700", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(6u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(5u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(50u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(6u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.96", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4236.0", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4225", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("00000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.700", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(6u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(10u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(75u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(6u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from(4235970u32);
dec.multiply_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4235.96", dec.to_string());
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("4236.0", dec.to_string());
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("4225", dec.to_string());
dec.round_with_mode_and_increment(
5,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("00000", dec.to_string());
dec.round_with_mode_and_increment(
2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("00000", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-99.999").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-100.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1234.56").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1234.6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.009").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.60").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.40").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.7000000099").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0.700", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(8u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf5,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(10u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(70u32);
dec.multiply_pow10(i16::MIN);
dec.round_with_mode_and_increment(
i16::MIN,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
let expected_min = {
let mut expected_min = SignedFixedDecimal::from(75u32);
expected_min.multiply_pow10(i16::MIN);
expected_min
};
assert_eq!(expected_min, dec);
let mut dec = SignedFixedDecimal::from(7u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(8u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from_str("1.108").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("1.12", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.108").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("1.15", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.108").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("1.25", dec.to_string());
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MAX - 1);
dec.round_with_mode_and_increment(
i16::MAX - 1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
let expected_max_minus_1 = {
let mut expected_max_minus_1 = SignedFixedDecimal::from(25u32);
expected_max_minus_1.multiply_pow10(i16::MAX - 1);
expected_max_minus_1
};
assert_eq!(expected_max_minus_1, dec);
let mut dec = SignedFixedDecimal::from(9u32);
dec.multiply_pow10(i16::MAX);
dec.round_with_mode_and_increment(
i16::MAX,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
let expected_max = {
let mut expected_max = SignedFixedDecimal::from(0u32);
expected_max.multiply_pow10(i16::MAX);
expected_max
};
assert_eq!(expected_max, dec);
let mut dec = SignedFixedDecimal::from_str("0").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("25", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("25", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("4").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("4").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("4.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("4.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5.1").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("10", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("6").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("6", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("6").unwrap();
dec.round_with_mode_and_increment(
0,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("10", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.50").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("0.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.1025").unwrap();
dec.round_with_mode_and_increment(
-3,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("1.100", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.10125").unwrap();
dec.round_with_mode_and_increment(
-4,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("1.1025", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-1.25").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("-1.0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-1.251").unwrap();
dec.round_with_mode_and_increment(
-1,
SignedRoundingMode::HalfCeil,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("-1.5", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-1.125").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::HalfFloor,
RoundingIncrement::MultiplesOf25,
);
assert_eq!("-1.25", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.71").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.72", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.73").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.72", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.75").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.76", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.77").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.76", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.79").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.80", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.41").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.40", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.43").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.44", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.45").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.44", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.47").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.48", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.49").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf2,
);
assert_eq!("2.48", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.725").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("2.70", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.775").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf5,
);
assert_eq!("2.80", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.875").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("3.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.375").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("2.50", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.125").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("2.00", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("2.625").unwrap();
dec.round_with_mode_and_increment(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
RoundingIncrement::MultiplesOf25,
);
assert_eq!("2.50", dec.to_string());
}