Skip to content

Commit

Permalink
Merge branch 'decimal2f64_normalization' into 'main'
Browse files Browse the repository at this point in the history
Normalize decimal before convert to f64.

See merge request codbase/decimal-rs!5
  • Loading branch information
davidli2010 committed May 24, 2021
2 parents 2314ca4 + d8c35f3 commit fe8cb1d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,16 @@ impl From<&Decimal> for f64 {
#[allow(clippy::comparison_chain)]
#[inline]
fn from(val: &Decimal) -> Self {
let mut v = val.int_val as f64;
let n = val.normalize();
let mut v = n.int_val as f64;

if val.scale > 0 {
v /= 10f64.powf(val.scale as f64);
} else if val.scale < 0 {
v *= 10f64.powf(-val.scale as f64);
if n.scale > 0 {
v /= 10f64.powf(n.scale as f64);
} else if n.scale < 0 {
v *= 10f64.powf(-n.scale as f64);
}

if val.negative {
if n.negative {
v = -v;
}

Expand Down Expand Up @@ -817,6 +818,8 @@ mod tests {
"-2145.5294117647058823529411764705882353",
-2145.5294117647059f64,
);
assert_into("7661.049086167562", 7661.049086167562f64);
assert_into("7661049086167562000e-15", 7661.049086167562f64);
}

#[test]
Expand Down

0 comments on commit fe8cb1d

Please sign in to comment.