+ 3
how can i split numbers thee to three in python
for example 1235633 1,235,633
5 Réponses
+ 4
Or:
print('{:,}'.format(1000000))
+ 8
Found these in a SO thread, more can be found in there, I just gathered some for my own curiosity, because I found number formatting is interesting, and important ; )
inum = 1235633
fnum = 1235633.123
print("{:,}".format(inum)) # int
print("{:,}".format(fnum)) # float
print(format(inum, ',d')) # int
print(format(fnum, ',.3f')) # float
print(f"{inum:,d}") # int
print(f"{fnum:,.3f}") # float
# Source:
# https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators
Hth, cmiiw
+ 4
Idea: Convert it into a string. Then make a list by slicing it into chunks of three (from the right). Finally, join the elements of the list with commas.
Try to implement it now. If you get stuck, show your attempt, and we'd be happy to help you further.
+ 3
Isn't our beloved Python always? ;-)
+ 1
Cool! That's convenient! 😊