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

Function format_general

crates/literal/src/float.rs:126–160  ·  view source on GitHub ↗
(
    precision: usize,
    magnitude: f64,
    case: Case,
    alternate_form: bool,
    always_shows_fract: bool,
)

Source from the content-addressed store, hash-verified

124}
125
126pub fn format_general(
127 precision: usize,
128 magnitude: f64,
129 case: Case,
130 alternate_form: bool,
131 always_shows_fract: bool,
132) -> String {
133 match magnitude {
134 magnitude if magnitude.is_finite() => {
135 let r_exp = format!("{:.*e}", precision.saturating_sub(1), magnitude);
136 let mut parts = r_exp.splitn(2, 'e');
137 let base = parts.next().unwrap();
138 let exponent = parts.next().unwrap().parse::<i64>().unwrap();
139 if exponent < -4 || exponent + (always_shows_fract as i64) >= (precision as i64) {
140 let e = match case {
141 Case::Lower => 'e',
142 Case::Upper => 'E',
143 };
144 let magnitude = format!("{:.*}", precision + 1, base);
145 let base = maybe_remove_trailing_redundant_chars(magnitude, alternate_form);
146 let point = decimal_point_or_empty(precision.saturating_sub(1), alternate_form);
147 format!("{base}{point}{e}{exponent:+#03}")
148 } else {
149 let precision = ((precision as i64) - 1 - exponent) as usize;
150 let magnitude = format!("{magnitude:.precision$}");
151 let base = maybe_remove_trailing_redundant_chars(magnitude, alternate_form);
152 let point = decimal_point_or_empty(precision, alternate_form);
153 format!("{base}{point}")
154 }
155 }
156 magnitude if magnitude.is_nan() => format_nan(case),
157 magnitude if magnitude.is_infinite() => format_inf(case),
158 _ => "".to_string(),
159 }
160}
161
162// TODO: rewrite using format_general
163pub fn to_string(value: f64) -> String {

Callers 4

format_float_localeMethod · 0.85
format_floatMethod · 0.85
format_complex_floatMethod · 0.85
format_floatMethod · 0.85

Calls 11

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