Skip to content
Merged
Next Next commit
N and n support for float format.
  • Loading branch information
JazzGlobal committed Apr 7, 2023
commit c1a390bee918f045272c46cbf166070544ed4419
23 changes: 23 additions & 0 deletions common/src/float_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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
{
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];

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

fn get_num_of_char(text: &String, value: char) -> i32 {
let mut total = 0;
for char in text.chars() {
if char == value { total += 1 }
}
total
}

pub fn to_string(value: f64) -> String {
let lit = format!("{value:e}");
if let Some(position) = lit.find('e') {
Expand Down
21 changes: 13 additions & 8 deletions common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub enum FormatType {
Character,
Decimal,
Octal,
Number,
Number(Case),
Hex(Case),
Exponent(Case),
GeneralFormat(Case),
Expand All @@ -142,7 +142,8 @@ impl From<&FormatType> for char {
FormatType::Character => 'c',
FormatType::Decimal => 'd',
FormatType::Octal => 'o',
FormatType::Number => 'n',
FormatType::Number(Case::Lower) => 'n',
FormatType::Number(Case::Upper) => 'N',
FormatType::Hex(Case::Lower) => 'x',
FormatType::Hex(Case::Upper) => 'X',
FormatType::Exponent(Case::Lower) => 'e',
Expand All @@ -165,7 +166,8 @@ impl FormatParse for FormatType {
Some('c') => (Some(Self::Character), chars.as_str()),
Some('d') => (Some(Self::Decimal), chars.as_str()),
Some('o') => (Some(Self::Octal), chars.as_str()),
Some('n') => (Some(Self::Number), chars.as_str()),
Some('n') => (Some(Self::Number(Case::Lower)), chars.as_str()),
Some('N') => (Some(Self::Number(Case::Upper)), chars.as_str()),
Some('x') => (Some(Self::Hex(Case::Lower)), chars.as_str()),
Some('X') => (Some(Self::Hex(Case::Upper)), chars.as_str()),
Some('e') => (Some(Self::Exponent(Case::Lower)), chars.as_str()),
Expand Down Expand Up @@ -367,14 +369,14 @@ impl FormatSpec {
| FormatType::Binary
| FormatType::Octal
| FormatType::Hex(_)
| FormatType::Number,
| FormatType::Number(_),
) => {
let ch = char::from(format_type);
Err(FormatSpecError::UnspecifiedFormat(',', ch))
}
(
Some(FormatGrouping::Underscore),
FormatType::String | FormatType::Character | FormatType::Number,
FormatType::String | FormatType::Character | FormatType::Number(_),
) => {
let ch = char::from(format_type);
Err(FormatSpecError::UnspecifiedFormat('_', ch))
Expand All @@ -386,7 +388,7 @@ impl FormatSpec {
fn get_separator_interval(&self) -> usize {
match self.format_type {
Some(FormatType::Binary | FormatType::Octal | FormatType::Hex(_)) => 4,
Some(FormatType::Decimal | FormatType::Number | FormatType::FixedPoint(_)) => 3,
Some(FormatType::Decimal | FormatType::Number(_) | FormatType::FixedPoint(_)) => 3,
None => 3,
_ => panic!("Separators only valid for numbers!"),
}
Expand Down Expand Up @@ -434,7 +436,10 @@ impl FormatSpec {
let ch = char::from(self.format_type.as_ref().unwrap());
Err(FormatSpecError::UnknownFormatCode(ch, "float"))
}
Some(FormatType::Number) => Err(FormatSpecError::NotImplemented('n', "float")),
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 Expand Up @@ -531,7 +536,7 @@ impl FormatSpec {
Ok(result)
}
},
Some(FormatType::Number) => self.format_int_radix(magnitude, 10),
Some(FormatType::Number(_)) => self.format_int_radix(magnitude, 10),
Some(FormatType::String) => Err(FormatSpecError::UnknownFormatCode('s', "int")),
Some(FormatType::Character) => match (self.sign, self.alternate_form) {
(Some(_), _) => Err(FormatSpecError::NotAllowed("Sign")),
Expand Down