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

Method format_complex_float

crates/common/src/format.rs:901–972  ·  view source on GitHub ↗
(&self, num: f64)

Source from the content-addressed store, hash-verified

899 }
900
901 fn format_complex_float(&self, num: f64) -> Result<String, FormatSpecError> {
902 self.validate_format(FormatType::FixedPoint(Case::Lower))?;
903 let precision = self.precision.unwrap_or(6);
904 let magnitude = num.abs();
905 let magnitude_str = match &self.format_type {
906 Some(FormatType::Decimal)
907 | Some(FormatType::Binary)
908 | Some(FormatType::Octal)
909 | Some(FormatType::Hex(_))
910 | Some(FormatType::String)
911 | Some(FormatType::Character)
912 | Some(FormatType::Number(Case::Upper))
913 | Some(FormatType::Percentage)
914 | Some(FormatType::Unknown(_)) => {
915 let ch = char::from(self.format_type.as_ref().unwrap());
916 Err(FormatSpecError::UnknownFormatCode(ch, "complex"))
917 }
918 Some(FormatType::FixedPoint(case)) => Ok(float::format_fixed(
919 precision,
920 magnitude,
921 *case,
922 self.alternate_form,
923 )),
924 Some(FormatType::GeneralFormat(case)) | Some(FormatType::Number(case)) => {
925 let precision = if precision == 0 { 1 } else { precision };
926 Ok(float::format_general(
927 precision,
928 magnitude,
929 *case,
930 self.alternate_form,
931 false,
932 ))
933 }
934 Some(FormatType::Exponent(case)) => Ok(float::format_exponent(
935 precision,
936 magnitude,
937 *case,
938 self.alternate_form,
939 )),
940 None => match magnitude {
941 magnitude if magnitude.is_nan() => Ok("nan".to_owned()),
942 magnitude if magnitude.is_infinite() => Ok("inf".to_owned()),
943 _ => match self.precision {
944 Some(precision) => Ok(float::format_general(
945 precision,
946 magnitude,
947 Case::Lower,
948 self.alternate_form,
949 true,
950 )),
951 None => {
952 if magnitude.fract() == 0.0 {
953 Ok(magnitude.trunc().to_string())
954 } else {
955 Ok(magnitude.to_string())
956 }
957 }
958 },

Callers 1

format_complex_re_imMethod · 0.80

Calls 15

format_fixedFunction · 0.85
format_generalFunction · 0.85
format_exponentFunction · 0.85
validate_formatMethod · 0.80
absMethod · 0.80
to_stringMethod · 0.80
truncMethod · 0.80
ErrClass · 0.50
unwrapMethod · 0.45
as_refMethod · 0.45
is_nanMethod · 0.45

Tested by

no test coverage detected