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

Function to_string

crates/literal/src/float.rs:163–183  ·  view source on GitHub ↗

TODO: rewrite using format_general

(value: f64)

Source from the content-addressed store, hash-verified

161
162// TODO: rewrite using format_general
163pub fn to_string(value: f64) -> String {
164 let lit = format!("{value:e}");
165 if let Some(position) = lit.find('e') {
166 let significand = &lit[..position];
167 let exponent = &lit[position + 1..];
168 let exponent = exponent.parse::<i32>().unwrap();
169 if exponent < 16 && exponent > -5 {
170 if is_integer(value) {
171 format!("{value:.1?}")
172 } else {
173 value.to_string()
174 }
175 } else {
176 format!("{significand}e{exponent:+#03}")
177 }
178 } else {
179 let mut s = value.to_string();
180 s.make_ascii_lowercase();
181 s
182 }
183}
184
185pub fn from_hex(s: &str) -> Option<f64> {
186 if let Ok(f) = hexf_parse::parse_hexf64(s, false) {

Callers 4

format_floatMethod · 0.50
repr_strMethod · 0.50
repr_strMethod · 0.50
unparse_exprMethod · 0.50

Calls 5

is_integerFunction · 0.85
to_stringMethod · 0.80
make_ascii_lowercaseMethod · 0.80
findMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected