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

Function format_exponent

crates/literal/src/float.rs:72–95  ·  view source on GitHub ↗

Formats floats into Python style exponent notation, by first formatting in Rust style exponent notation (`1.0000e0`), then convert to Python style (`1.0000e+00`).

(
    precision: usize,
    magnitude: f64,
    case: Case,
    alternate_form: bool,
)

Source from the content-addressed store, hash-verified

70// Formats floats into Python style exponent notation, by first formatting in Rust style
71// exponent notation (`1.0000e0`), then convert to Python style (`1.0000e+00`).
72pub fn format_exponent(
73 precision: usize,
74 magnitude: f64,
75 case: Case,
76 alternate_form: bool,
77) -> String {
78 match magnitude {
79 magnitude if magnitude.is_finite() => {
80 let r_exp = format!("{magnitude:.precision$e}");
81 let mut parts = r_exp.splitn(2, 'e');
82 let base = parts.next().unwrap();
83 let exponent = parts.next().unwrap().parse::<i64>().unwrap();
84 let e = match case {
85 Case::Lower => 'e',
86 Case::Upper => 'E',
87 };
88 let point = decimal_point_or_empty(precision, alternate_form);
89 format!("{base}{point}{e}{exponent:+#03}")
90 }
91 magnitude if magnitude.is_nan() => format_nan(case),
92 magnitude if magnitude.is_infinite() => format_inf(case),
93 _ => "".to_string(),
94 }
95}
96
97/// If s represents a floating point value, trailing zeros and a possibly trailing
98/// decimal point will be removed.

Callers 3

format_floatMethod · 0.85
format_complex_floatMethod · 0.85
format_floatMethod · 0.85

Calls 10

decimal_point_or_emptyFunction · 0.85
format_nanFunction · 0.85
format_infFunction · 0.85
splitnMethod · 0.80
to_stringMethod · 0.80
is_finiteMethod · 0.45
unwrapMethod · 0.45
nextMethod · 0.45
is_nanMethod · 0.45
is_infiniteMethod · 0.45

Tested by

no test coverage detected