Skip to content

Commit 814018f

Browse files
committed
Fix float.__repr__() method
1 parent fb206a0 commit 814018f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

vm/src/obj/objfloat.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,20 @@ impl PyFloat {
416416

417417
#[pymethod(name = "__repr__")]
418418
fn repr(&self, vm: &VirtualMachine) -> String {
419-
if self.is_integer(vm) {
420-
format!("{:.1?}", self.value)
419+
let value = format!("{:e}", self.value);
420+
if let Some(position) = value.find('e') {
421+
let significand = &value[..position];
422+
let exponent = &value[position + 1..];
423+
let exponent = exponent.parse::<i32>().unwrap();
424+
if exponent < 16 && exponent > -5 {
425+
if self.is_integer(vm) {
426+
format!("{:.1?}", self.value)
427+
} else {
428+
self.value.to_string()
429+
}
430+
} else {
431+
format!("{}e{:+#03}", significand, exponent)
432+
}
421433
} else {
422434
self.value.to_string()
423435
}

0 commit comments

Comments
 (0)