MCPcopy Index your code
hub / github.com/RustPython/RustPython / format_float_locale

Method format_float_locale

crates/common/src/format.rs:568–605  ·  view source on GitHub ↗

Format a float with locale-aware 'n' format.

(
        &self,
        num: f64,
        locale: &LocaleInfo,
    )

Source from the content-addressed store, hash-verified

566
567 /// Format a float with locale-aware 'n' format.
568 pub fn format_float_locale(
569 &self,
570 num: f64,
571 locale: &LocaleInfo,
572 ) -> Result<String, FormatSpecError> {
573 self.validate_format(FormatType::FixedPoint(Case::Lower))?;
574 let precision = self.precision.unwrap_or(6);
575 let magnitude = num.abs();
576
577 let raw_magnitude_str = match &self.format_type {
578 Some(FormatType::Number(case)) => {
579 let precision = if precision == 0 { 1 } else { precision };
580 Ok(float::format_general(
581 precision,
582 magnitude,
583 *case,
584 self.alternate_form,
585 false,
586 ))
587 }
588 _ => return self.format_float(num),
589 }?;
590
591 let magnitude_str = Self::apply_locale_formatting(raw_magnitude_str, locale);
592
593 let format_sign = self.sign.unwrap_or(FormatSign::Minus);
594 let sign_str = if num.is_sign_negative() && !num.is_nan() {
595 "-"
596 } else {
597 match format_sign {
598 FormatSign::Plus => "+",
599 FormatSign::Minus => "",
600 FormatSign::MinusOrSpace => " ",
601 }
602 };
603
604 self.format_sign_and_align(&AsciiStr::new(&magnitude_str), sign_str, FormatAlign::Right)
605 }
606
607 /// Format a complex number with locale-aware 'n' format.
608 pub fn format_complex_locale(

Callers 1

__format__Method · 0.80

Calls 7

format_generalFunction · 0.85
newFunction · 0.85
validate_formatMethod · 0.80
absMethod · 0.80
format_sign_and_alignMethod · 0.80
format_floatMethod · 0.45
is_nanMethod · 0.45

Tested by

no test coverage detected