Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round f64 before final conversion in to_f64 #408

Merged
merged 3 commits into from Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/decimal.rs
Expand Up @@ -1841,7 +1841,7 @@ impl ToPrimitive for Decimal {
let frac_f64 = (frac_part as f64) / (precision as f64);
let value = sign * ((integral_part as f64) + frac_f64);
let round_to = 10f64.powi(self.scale() as i32);
Some(value * round_to / round_to)
Some((value * round_to).round() / round_to)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/decimal_tests.rs
Expand Up @@ -2457,6 +2457,9 @@ fn it_converts_to_f64() {
None, // Cannot be represented in an f64
),
("1.59283191", Some(1.59283191_f64)),
("2.2238", Some(2.2238_f64)),
("2.2238123", Some(2.2238123_f64)),
("22238", Some(22238_f64)),
];
for &(value, expected) in tests {
let value = Decimal::from_str(value).unwrap().to_f64();
Expand Down