Skip to content

Commit 6a15801

Browse files
committed
Implement the '%' string format code for integers
1 parent cdba57d commit 6a15801

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

tests/snippets/strings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,11 @@ def try_mutate_str():
433433
f'{5.0:04d}'
434434

435435
# Test % formatting
436+
assert f'{10:%}' == '1000.000000%'
436437
assert f'{10.0:%}' == '1000.000000%'
437438
assert f'{10.0:.2%}' == '1000.00%'
438439
assert f'{10.0:.8%}' == '1000.00000000%'
440+
assert f'{-10:%}' == '-1000.000000%'
439441
assert f'{-10.0:%}' == '-1000.000000%'
440442
assert f'{-10.0:.2%}' == '-1000.00%'
441443
assert f'{-10.0:.8%}' == '-1000.00000000%'

vm/src/format.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,12 @@ impl FormatSpec {
447447
Some(FormatType::ExponentLower) => {
448448
Err("Unknown format code 'e' for object of type 'int'")
449449
}
450-
Some(FormatType::FixedPointUpper) | Some(FormatType::FixedPointLower) => {
451-
match num.to_f64() {
452-
Some(float) => return self.format_float(float),
453-
_ => Err("Unable to convert int to float"),
454-
}
455-
}
456-
Some(FormatType::Percentage) => {
457-
Err("Format code '%' for object of type 'int' not implemented yet")
458-
}
450+
Some(FormatType::FixedPointUpper)
451+
| Some(FormatType::FixedPointLower)
452+
| Some(FormatType::Percentage) => match num.to_f64() {
453+
Some(float) => return self.format_float(float),
454+
_ => Err("Unable to convert int to float"),
455+
},
459456
None => Ok(magnitude.to_str_radix(10)),
460457
};
461458
if raw_magnitude_string_result.is_err() {

0 commit comments

Comments
 (0)