bigdecimal-rs
bigdecimal-rs copied to clipboard
Inconsistent formatting of scientific notation for powers of 10
Version 0.4.8. Values that round up to a power of 10 do not have the same scientific notation as values that round down, or round to a non power of 10. e.g:
let sf = NonZeroU64::new(1).unwrap();
let d1 = BigDecimal::from_str("89.9")
.unwrap()
.with_precision_round(sf, RoundingMode::HalfUp);
let d2 = BigDecimal::from_str("99.9")
.unwrap()
.with_precision_round(sf, RoundingMode::HalfUp);
let d3 = BigDecimal::from_str("100.0")
.unwrap()
.with_precision_round(sf, RoundingMode::HalfUp);
let d4 = BigDecimal::from_str("101.0")
.unwrap()
.with_precision_round(sf, RoundingMode::HalfUp);
println!("{}:{}", d1, d1.to_scientific_notation());
println!("{}:{}", d2, d2.to_scientific_notation());
println!("{}:{}", d3, d3.to_scientific_notation());
println!("{}:{}", d4, d4.to_scientific_notation());
Outputs:
90:9e1
100:1.0e2
100:1e2
100:1e2
Similarly, for 2 significant figures:
90:9.0e1
100:1.00e2
100:1.0e2
100:1.0e2