forked from Rust-related/RustPython
#1224 Add integer supporting for float formatting
This commit is contained in:
@@ -831,6 +831,20 @@ mod tests {
|
||||
.format_float(f64::from(1.2345678901)),
|
||||
"1.234568".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"%f"
|
||||
.parse::<CFormatSpec>()
|
||||
.unwrap()
|
||||
.format_number(&BigInt::from(123)),
|
||||
"123.000000".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"%f"
|
||||
.parse::<CFormatSpec>()
|
||||
.unwrap()
|
||||
.format_number(&BigInt::from(-123)),
|
||||
"-123.000000".to_string()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user