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

Support precision in fmt_scientific_notation #555

Merged
merged 3 commits into from
Nov 24, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,30 @@ pub(crate) fn fmt_scientific_notation(
// point in the right place and adjust the exponent accordingly.

let len = chars.len();
let precision = f.precision().unwrap_or(0);
let mut rep;
if len > 1 {
if chars.iter().take(len - 1).all(|c| *c == '0') {
// Chomp off the zero's.
if precision == 0 && chars.iter().take(len - 1).all(|c| *c == '0') {
rep = chars.iter().skip(len - 1).collect::<String>();
} else {
chars.insert(len - 1, '.');
rep = chars.iter().rev().collect::<String>();
if precision > 0 || f.precision().is_none() {
chars.insert(len - 1, '.');
}
rep = chars
.iter()
.rev()
.chain(core::iter::repeat(&'0'))
.take(f.precision().map(|p| if p == 0 { 1 } else { 2 + p }).unwrap_or(len + 1))
.collect::<String>();
}
exponent += (len - 1) as isize;
} else if precision > 0 {
chars.push('.');
rep = chars
.iter()
.chain(core::iter::repeat(&'0'))
.take(2 + precision)
.collect::<String>();
} else {
rep = chars.iter().collect::<String>();
}
Expand Down
89 changes: 89 additions & 0 deletions tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,95 @@ fn it_formats_lower_exp_padding() {
}
}

#[test]
fn it_formats_scientific_precision() {
for (num, scale, expected_no_precision, expected_precision) in [
(
123456,
10,
"1.23456e-5",
[
"1e-5",
"1.2e-5",
"1.23e-5",
"1.234e-5",
"1.2345e-5",
"1.23456e-5",
"1.234560e-5",
"1.2345600e-5",
],
),
(
123456,
0,
"1.23456e5",
[
"1e5",
"1.2e5",
"1.23e5",
"1.234e5",
"1.2345e5",
"1.23456e5",
"1.234560e5",
"1.2345600e5",
],
),
(
1,
0,
"1e0",
[
"1e0",
"1.0e0",
"1.00e0",
"1.000e0",
"1.0000e0",
"1.00000e0",
"1.000000e0",
"1.0000000e0",
],
),
(
-123456,
10,
"-1.23456e-5",
[
"-1e-5",
"-1.2e-5",
"-1.23e-5",
"-1.234e-5",
"-1.2345e-5",
"-1.23456e-5",
"-1.234560e-5",
"-1.2345600e-5",
],
),
(
-100000,
10,
"-1e-5",
[
"-1e-5",
"-1.0e-5",
"-1.00e-5",
"-1.000e-5",
"-1.0000e-5",
"-1.00000e-5",
"-1.000000e-5",
"-1.0000000e-5",
],
),
] {
assert_eq!(format!("{:e}", Decimal::new(num, scale)), expected_no_precision);
for i in 0..expected_precision.len() {
assert_eq!(
format!("{:.prec$e}", Decimal::new(num, scale), prec = i),
expected_precision[i]
);
}
}
}

// Negation
#[test]
fn it_negates_decimals() {
Expand Down