Skip to content

Commit

Permalink
py/mpz: Skip separators when running out of digits to print.
Browse files Browse the repository at this point in the history
This commit fixes the addition of a stray separator before the number
when printing an MPZ-backed integer and the first group is three digits
long.

This fixes micropython#8984.

Signed-off-by: Alessandro Gatti <[email protected]>
  • Loading branch information
agatti authored and dpgeorge committed Sep 26, 2024
1 parent b0ba151 commit 43b05af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py/mpz.c
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,7 @@ size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, ch
break;
}
}
if (comma && (s - last_comma) == 3) {
if (!done && comma && (s - last_comma) == 3) {
*s++ = comma;
last_comma = s;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/basics/string_format_intbig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# basic functionality test for {} format string using large integers


def test(fmt, *args):
print("{:8s}".format(fmt) + ">" + fmt.format(*args) + "<")


# Separator formatter

test("{:,}", 123_456_789_012_345_678_901_234_567)
test("{:,}", 23_456_789_012_345_678_901_234_567)
test("{:,}", 3_456_789_012_345_678_901_234_567)
test("{:,}", -123_456_789_012_345_678_901_234_567)
test("{:,}", -23_456_789_012_345_678_901_234_567)
test("{:,}", -3_456_789_012_345_678_901_234_567)

0 comments on commit 43b05af

Please sign in to comment.