#1224 Add integer supporting for float formatting

This commit is contained in:
MinGyo Jung
2019-08-15 18:53:46 +09:00
parent e1ac729770
commit 26168bc1e5
2 changed files with 22 additions and 5 deletions

View File

@@ -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]

View File

@@ -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 = {