diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index 0d86609bf..e372384a8 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -831,6 +831,20 @@ mod tests { .format_float(f64::from(1.2345678901)), "1.234568".to_string() ); + assert_eq!( + "%f" + .parse::() + .unwrap() + .format_number(&BigInt::from(123)), + "123.000000".to_string() + ); + assert_eq!( + "%f" + .parse::() + .unwrap() + .format_number(&BigInt::from(-123)), + "-123.000000".to_string() + ) } #[test] diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 95b4d0f05..1e05fb862 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -1222,16 +1222,19 @@ fn do_cformat_specifier( Ok(format_spec.format_number(objint::get_value(&obj))) } CFormatType::Float(_) => { - if !objtype::isinstance(&obj, &vm.ctx.float_type()) { - let required_type_string = "an floating point"; - return Err(vm.new_type_error(format!( + if objtype::isinstance(&obj, &vm.ctx.float_type()) { + Ok(format_spec.format_float(objfloat::get_value(&obj))) + } else if objtype::isinstance(&obj, &vm.ctx.int_type()){ + Ok(format_spec.format_float(objint::get_value(&obj).to_f64().unwrap())) + } else { + let required_type_string = "an floating point or integer"; + Err(vm.new_type_error(format!( "%{} format: {} is required, not {}", format_spec.format_char, required_type_string, obj.class() - ))); + ))) } - Ok(format_spec.format_float(objfloat::get_value(&obj))) } CFormatType::Character => { let char_string = {