Merge pull request #1551 from ChJR/feature/float-repr

Fix float.__repr__()
This commit is contained in:
Windel Bouwman
2019-10-20 21:14:09 +02:00
committed by GitHub
2 changed files with 25 additions and 2 deletions

View File

@@ -416,8 +416,20 @@ impl PyFloat {
#[pymethod(name = "__repr__")]
fn repr(&self, vm: &VirtualMachine) -> String {
if self.is_integer(vm) {
format!("{:.1?}", self.value)
let value = format!("{:e}", self.value);
if let Some(position) = value.find('e') {
let significand = &value[..position];
let exponent = &value[position + 1..];
let exponent = exponent.parse::<i32>().unwrap();
if exponent < 16 && exponent > -5 {
if self.is_integer(vm) {
format!("{:.1?}", self.value)
} else {
self.value.to_string()
}
} else {
format!("{}e{:+#03}", significand, exponent)
}
} else {
self.value.to_string()
}