Skip to content

Commit 43b05af

Browse files
agattidpgeorge
authored andcommittedSep 26, 2024
py/mpz: Skip separators when running out of digits to print.
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]>
1 parent b0ba151 commit 43b05af

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
 

‎py/mpz.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, ch
17171717
break;
17181718
}
17191719
}
1720-
if (comma && (s - last_comma) == 3) {
1720+
if (!done && comma && (s - last_comma) == 3) {
17211721
*s++ = comma;
17221722
last_comma = s;
17231723
}

‎tests/basics/string_format_intbig.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# basic functionality test for {} format string using large integers
2+
3+
4+
def test(fmt, *args):
5+
print("{:8s}".format(fmt) + ">" + fmt.format(*args) + "<")
6+
7+
8+
# Separator formatter
9+
10+
test("{:,}", 123_456_789_012_345_678_901_234_567)
11+
test("{:,}", 23_456_789_012_345_678_901_234_567)
12+
test("{:,}", 3_456_789_012_345_678_901_234_567)
13+
test("{:,}", -123_456_789_012_345_678_901_234_567)
14+
test("{:,}", -23_456_789_012_345_678_901_234_567)
15+
test("{:,}", -3_456_789_012_345_678_901_234_567)

0 commit comments

Comments
 (0)
Please sign in to comment.