@@ -582,27 +582,26 @@ impl BigDecimal {
582
582
}
583
583
584
584
/// Return number rounded to round_digits precision after the decimal point
585
- /// panic if self.digits() > 16
586
585
fn round ( & self , round_digits : i64 ) -> BigDecimal {
587
586
let ( bigint, decimal_part_digits) = self . as_bigint_and_exponent ( ) ;
588
587
let need_to_round_digits = decimal_part_digits - round_digits;
589
588
if round_digits >= 0 && need_to_round_digits <= 0 {
590
589
return self . clone ( ) ;
591
590
}
592
591
593
- let mut number = bigint. to_i64 ( ) . unwrap ( ) ;
594
- // avoid -1555 / 10 = -156
592
+ let mut number = bigint. to_i128 ( ) . unwrap ( ) ;
595
593
if number < 0 {
596
594
number = -number;
597
595
}
598
- for _ in 0 ..( need_to_round_digits- 1 ) {
596
+ for _ in 0 ..( need_to_round_digits - 1 ) {
599
597
number /= 10 ;
600
598
}
601
599
let digit = number % 10 ;
600
+
602
601
if digit <= 4 {
603
602
self . with_scale ( round_digits)
604
603
} else {
605
- if bigint. sign ( ) == Sign :: Minus {
604
+ if bigint. is_negative ( ) {
606
605
self . with_scale ( round_digits) - BigDecimal :: new ( BigInt :: from ( 1 ) , round_digits)
607
606
} else {
608
607
self . with_scale ( round_digits) + BigDecimal :: new ( BigInt :: from ( 1 ) , round_digits)
@@ -2583,6 +2582,7 @@ mod bigdecimal_tests {
2583
2582
( "-1.4499999999" , 1 , "-1.4" ) ,
2584
2583
( "1.449999999" , 1 , "1.4" ) ,
2585
2584
( "-9999.444455556666" , 10 , "-9999.4444555567" ) ,
2585
+ ( "-12345678987654321.123456789" , 8 , "-12345678987654321.12345679" ) ,
2586
2586
] ;
2587
2587
for & ( x, digits, y) in test_cases. iter ( ) {
2588
2588
let a = BigDecimal :: from_str ( x) . unwrap ( ) ;
0 commit comments