8000 'n' support for float format by JazzGlobal · Pull Request #4865 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

'n' support for float format #4865

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

Merged
merged 14 commits into from
Apr 15, 2023
Merged
Prev Previous commit
Next Next commit
cargo fmt
  • Loading branch information
JazzGlobal committed Apr 8, 2023
commit a937e8eefe42f0fdd32776a18d550fa5adf7c03f
21 changes: 14 additions & 7 deletions common/src/float_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,29 @@ pub fn format_general(
pub fn format_number(magnitude: f64, precision: usize) -> String {
let magnitude_as_string = magnitude.to_string();
let separator_index = magnitude_as_string.chars().position(|c| c == '.').unwrap();
if separator_index == precision { return String::from(&magnitude_as_string[..separator_index]) }
if separator_index < precision
{
if separator_index == precision {
return String::from(&magnitude_as_string[..separator_index]);
}
if separator_index < precision {
let prefix = &magnitude_as_string[0..separator_index];
let suffix_distance = (precision - prefix.len() + 1) + separator_index;
let suffix = &magnitude_as_string[separator_index..suffix_distance];
let suffix = &magnitude_as_string[separator_index..suffix_distance];

return format!("{}{}", prefix, suffix)
return format!("{}{}", prefix, suffix);
}
format!("{}e+0{}", &magnitude_as_string[..1], get_num_of_char(&magnitude_as_string, '0'))
format!(
"{}e+0{}",
&magnitude_as_string[..1],
get_num_of_char(&magnitude_as_string, '0')
)
}

fn get_num_of_char(text: &str, value: char) -> i32 {
let mut total = 0;
for char in text.chars() {
if char == value { total += 1 }
if char == value {
total += 1
}
}
total
}
Expand Down
2 changes: 1 addition & 1 deletion common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ impl FormatSpec {
Some(FormatType::Number(_)) => {
let precision = if precision == 0 { 6 } else { precision };
Ok(float_ops::format_number(magnitude, precision))
},
}
Some(FormatType::GeneralFormat(case)) => {
let precision = if precision == 0 { 1 } else { precision };
Ok(float_ops::format_general(
Expand Down
0