Skip to content

Commit 02ba26b

Browse files
committed
Rustfmt + fix panic on long fractions
1 parent fc797c3 commit 02ba26b

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/lib.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ use std::default::Default;
5252
use std::error::Error;
5353
use std::fmt;
5454
use std::hash::{Hash, Hasher};
55+
use std::iter::Sum;
5556
use std::num::{ParseFloatError, ParseIntError};
5657
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign};
57-
use std::iter::Sum;
5858
use std::str::{self, FromStr};
5959

6060
use num_bigint::{BigInt, ParseBigIntError, Sign, ToBigInt};
@@ -127,11 +127,6 @@ fn get_rounding_term(num: &BigInt) -> u8 {
127127
}
128128
n *= 2;
129129
}
130-
131-
// string-method
132-
// let s = format!("{}", num);
133-
// let high_digit = u8::from_str(&s[0..1]).unwrap();
134-
// if high_digit < 5 { 0 } else { 1 }
135130
}
136131

137132
/// A big decimal type.
@@ -229,7 +224,7 @@ impl BigDecimal {
229224

230225
// check for "leading zero" in remainder term; otherwise round
231226
if p < 10 * &r {
232-
q += get_rounding_term(&r);
227+
q += get_rounding_term(&r);
233228
}
234229

235230
BigDecimal {
@@ -593,16 +588,16 @@ impl BigDecimal {
593588
return self.clone();
594589
}
595590

596-
let mut number = bigint.to_i128().unwrap();
597-
if number < 0 {
591+
let mut number = bigint.clone();
592+
if number < BigInt::zero() {
598593
number = -number;
599594
}
600595
for _ in 0..(need_to_round_digits - 1) {
601596
number /= 10;
602597
}
603598
let digit = number % 10;
604599

605-
if digit <= 4 {
600+
if digit <= BigInt::from(4) {
606601
self.with_scale(round_digits)
607602
} else if bigint.is_negative() {
608603
self.with_scale(round_digits) - BigDecimal::new(BigInt::from(1), round_digits)
@@ -619,7 +614,7 @@ impl BigDecimal {
619614
if self.scale <= 0 {
620615
true
621616
} else {
622-
(self.int_val.clone() % ten_to_the(self.scale as u64)).is_zero()
617+
(self.int_val.clone() % ten_to_the(self.scale as u64)).is_zero()
623618
}
624619
}
625620

@@ -661,7 +656,7 @@ impl BigDecimal {
661656
#[must_use]
662657
pub fn normalized(&self) -> BigDecimal {
663658
if self == &BigDecimal::zero() {
664-
return BigDecimal::zero()
659+
return BigDecimal::zero();
665660
}
666661
let (sign, mut digits) = self.int_val.to_radix_be(10);
667662
let trailing_count = digits.iter().rev().take_while(|i| **i == 0).count();
@@ -729,7 +724,7 @@ impl FromStr for BigDecimal {
729724
}
730725
}
731726

732-
#[allow(deprecated)] // trim_right_match -> trim_end_match
727+
#[allow(deprecated)] // trim_right_match -> trim_end_match
733728
impl Hash for BigDecimal {
734729
fn hash<H: Hasher>(&self, state: &mut H) {
735730
let mut dec_str = self.int_val.to_str_radix(10).to_string();
@@ -1003,7 +998,8 @@ impl<'a> AddAssign<&'a BigInt> for BigDecimal {
1003998
match self.scale.cmp(&0) {
1004999
Ordering::Equal => self.int_val += rhs,
10051000
Ordering::Greater => self.int_val += rhs * ten_to_the(self.scale as u64),
1006-
Ordering::Less => { // *self += BigDecimal::new(rhs, 0)
1001+
Ordering::Less => {
1002+
// *self += BigDecimal::new(rhs, 0)
10071003
self.int_val *= ten_to_the((-self.scale) as u64);
10081004
self.int_val += rhs;
10091005
self.scale = 0;
@@ -1164,7 +1160,9 @@ impl<'a> SubAssign<&'a BigInt> for BigDecimal {
11641160
fn sub_assign(&mut self, rhs: &BigInt) {
11651161
match self.scale.cmp(&0) {
11661162
Ordering::Equal => SubAssign::sub_assign(&mut self.int_val, rhs),
1167-
Ordering::Greater => SubAssign::sub_assign(&mut self.int_val, rhs * ten_to_the(self.scale as u64)),
1163+
Ordering::Greater => {
1164+
SubAssign::sub_assign(&mut self.int_val, rhs * ten_to_the(self.scale as u64))
1165+
}
11681166
Ordering::Less => {
11691167
self.int_val *= ten_to_the((-self.scale) as u64);
11701168
SubAssign::sub_assign(&mut self.int_val, rhs);
@@ -1255,8 +1253,6 @@ impl<'a, 'b> Mul<&'a BigInt> for &'b BigDecimal {
12551253
}
12561254
}
12571255

1258-
1259-
12601256
forward_val_assignop!(impl MulAssign for BigDecimal, mul_assign);
12611257

12621258
impl<'a> MulAssign<&'a BigDecimal> for BigDecimal {
@@ -1544,14 +1540,14 @@ impl Signed for BigDecimal {
15441540

15451541
impl Sum for BigDecimal {
15461542
#[inline]
1547-
fn sum<I: Iterator<Item=BigDecimal>>(iter: I) -> BigDecimal {
1543+
fn sum<I: Iterator<Item = BigDecimal>>(iter: I) -> BigDecimal {
15481544
iter.fold(Zero::zero(), |a, b| a + b)
15491545
}
15501546
}
15511547

15521548
impl<'a> Sum<&'a BigDecimal> for BigDecimal {
15531549
#[inline]
1554-
fn sum<I: Iterator<Item=&'a BigDecimal>>(iter: I) -> BigDecimal {
1550+
fn sum<I: Iterator<Item = &'a BigDecimal>>(iter: I) -> BigDecimal {
15551551
iter.fold(Zero::zero(), |a, b| a + b)
15561552
}
15571553
}
@@ -1892,8 +1888,8 @@ mod bigdecimal_serde {
18921888
#[cfg(feature = "string-only")]
18931889
impl<'de> de::Deserialize<'de> for BigDecimal {
18941890
fn deserialize<D>(d: D) -> Result<Self, D::Error>
1895-
where
1896-
D: de::Deserializer<'de>,
1891+
where
1892+
D: de::Deserializer<'de>,
18971893
{
18981894
d.deserialize_str(BigDecimalVisitor)
18991895
}
@@ -2598,6 +2594,7 @@ mod bigdecimal_tests {
25982594
("1.449999999", 1, "1.4"),
25992595
("-9999.444455556666", 10, "-9999.4444555567"),
26002596
("-12345678987654321.123456789", 8, "-12345678987654321.12345679"),
2597+
("0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333", 0, "0"),
26012598
];
26022599
for &(x, digits, y) in test_cases.iter() {
26032600
let a = BigDecimal::from_str(x).unwrap();

0 commit comments

Comments
 (0)