@@ -52,9 +52,9 @@ use std::default::Default;
52
52
use std:: error:: Error ;
53
53
use std:: fmt;
54
54
use std:: hash:: { Hash , Hasher } ;
55
+ use std:: iter:: Sum ;
55
56
use std:: num:: { ParseFloatError , ParseIntError } ;
56
57
use std:: ops:: { Add , AddAssign , Div , Mul , MulAssign , Neg , Rem , Sub , SubAssign } ;
57
- use std:: iter:: Sum ;
58
58
use std:: str:: { self , FromStr } ;
59
59
60
60
use num_bigint:: { BigInt , ParseBigIntError , Sign , ToBigInt } ;
@@ -127,11 +127,6 @@ fn get_rounding_term(num: &BigInt) -> u8 {
127
127
}
128
128
n *= 2 ;
129
129
}
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 }
135
130
}
136
131
137
132
/// A big decimal type.
@@ -229,7 +224,7 @@ impl BigDecimal {
229
224
230
225
// check for "leading zero" in remainder term; otherwise round
231
226
if p < 10 * & r {
232
- q += get_rounding_term ( & r) ;
227
+ q += get_rounding_term ( & r) ;
233
228
}
234
229
235
230
BigDecimal {
@@ -593,16 +588,16 @@ impl BigDecimal {
593
588
return self . clone ( ) ;
594
589
}
595
590
596
- let mut number = bigint. to_i128 ( ) . unwrap ( ) ;
597
- if number < 0 {
591
+ let mut number = bigint. clone ( ) ;
592
+ if number < BigInt :: zero ( ) {
598
593
number = -number;
599
594
}
600
595
for _ in 0 ..( need_to_round_digits - 1 ) {
601
596
number /= 10 ;
602
597
}
603
598
let digit = number % 10 ;
604
599
605
- if digit <= 4 {
600
+ if digit <= BigInt :: from ( 4 ) {
606
601
self . with_scale ( round_digits)
607
602
} else if bigint. is_negative ( ) {
608
603
self . with_scale ( round_digits) - BigDecimal :: new ( BigInt :: from ( 1 ) , round_digits)
@@ -619,7 +614,7 @@ impl BigDecimal {
619
614
if self . scale <= 0 {
620
615
true
621
616
} 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 ( )
623
618
}
624
619
}
625
620
@@ -661,7 +656,7 @@ impl BigDecimal {
661
656
#[ must_use]
662
657
pub fn normalized ( & self ) -> BigDecimal {
663
658
if self == & BigDecimal :: zero ( ) {
664
- return BigDecimal :: zero ( )
659
+ return BigDecimal :: zero ( ) ;
665
660
}
666
661
let ( sign, mut digits) = self . int_val . to_radix_be ( 10 ) ;
667
662
let trailing_count = digits. iter ( ) . rev ( ) . take_while ( |i| * * i == 0 ) . count ( ) ;
@@ -729,7 +724,7 @@ impl FromStr for BigDecimal {
729
724
}
730
725
}
731
726
732
- #[ allow( deprecated) ] // trim_right_match -> trim_end_match
727
+ #[ allow( deprecated) ] // trim_right_match -> trim_end_match
733
728
impl Hash for BigDecimal {
734
729
fn hash < H : Hasher > ( & self , state : & mut H ) {
735
730
let mut dec_str = self . int_val . to_str_radix ( 10 ) . to_string ( ) ;
@@ -1003,7 +998,8 @@ impl<'a> AddAssign<&'a BigInt> for BigDecimal {
1003
998
match self . scale . cmp ( & 0 ) {
1004
999
Ordering :: Equal => self . int_val += rhs,
1005
1000
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)
1007
1003
self . int_val *= ten_to_the ( ( -self . scale ) as u64 ) ;
1008
1004
self . int_val += rhs;
1009
1005
self . scale = 0 ;
@@ -1164,7 +1160,9 @@ impl<'a> SubAssign<&'a BigInt> for BigDecimal {
1164
1160
fn sub_assign ( & mut self , rhs : & BigInt ) {
1165
1161
match self . scale . cmp ( & 0 ) {
1166
1162
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
+ }
1168
1166
Ordering :: Less => {
1169
1167
self . int_val *= ten_to_the ( ( -self . scale ) as u64 ) ;
1170
1168
SubAssign :: sub_assign ( & mut self . int_val , rhs) ;
@@ -1255,8 +1253,6 @@ impl<'a, 'b> Mul<&'a BigInt> for &'b BigDecimal {
1255
1253
}
1256
1254
}
1257
1255
1258
-
1259
-
1260
1256
forward_val_assignop ! ( impl MulAssign for BigDecimal , mul_assign) ;
1261
1257
1262
1258
impl < ' a > MulAssign < & ' a BigDecimal > for BigDecimal {
@@ -1544,14 +1540,14 @@ impl Signed for BigDecimal {
1544
1540
1545
1541
impl Sum for BigDecimal {
1546
1542
#[ inline]
1547
- fn sum < I : Iterator < Item = BigDecimal > > ( iter : I ) -> BigDecimal {
1543
+ fn sum < I : Iterator < Item = BigDecimal > > ( iter : I ) -> BigDecimal {
1548
1544
iter. fold ( Zero :: zero ( ) , |a, b| a + b)
1549
1545
}
1550
1546
}
1551
1547
1552
1548
impl < ' a > Sum < & ' a BigDecimal > for BigDecimal {
1553
1549
#[ inline]
1554
- fn sum < I : Iterator < Item = & ' a BigDecimal > > ( iter : I ) -> BigDecimal {
1550
+ fn sum < I : Iterator < Item = & ' a BigDecimal > > ( iter : I ) -> BigDecimal {
1555
1551
iter. fold ( Zero :: zero ( ) , |a, b| a + b)
1556
1552
}
1557
1553
}
@@ -1892,8 +1888,8 @@ mod bigdecimal_serde {
1892
1888
#[ cfg( feature = "string-only" ) ]
1893
1889
impl < ' de > de:: Deserialize < ' de > for BigDecimal {
1894
1890
fn deserialize < D > ( d : D ) -> Result < Self , D :: Error >
1895
- where
1896
- D : de:: Deserializer < ' de > ,
1891
+ where
1892
+ D : de:: Deserializer < ' de > ,
1897
1893
{
1898
1894
d. deserialize_str ( BigDecimalVisitor )
1899
1895
}
@@ -2598,6 +2594,7 @@ mod bigdecimal_tests {
2598
2594
( "1.449999999" , 1 , "1.4" ) ,
2599
2595
( "-9999.444455556666" , 10 , "-9999.4444555567" ) ,
2600
2596
( "-12345678987654321.123456789" , 8 , "-12345678987654321.12345679" ) ,
2597
+ ( "0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333" , 0 , "0" ) ,
2601
2598
] ;
2602
2599
for & ( x, digits, y) in test_cases. iter ( ) {
2603
2600
let a = BigDecimal :: from_str ( x) . unwrap ( ) ;
0 commit comments