E X E R C I S E # 3 3 : C O M M A - F O R M A T T E D N U M B E R S commaFormat(12345)
→
'12,345'
In the US and UK, the digits of numbers are grouped with commas every three digits. For
example, the number 79033516 is written as 79,033,516 for readability. In this exercise, you’ll write a
function that takes a number and returns a string of the number with comma formatting.
Exercise Description Write a commaFormat() function with a number parameter. The argument for this parameter
can be an integer or floating-point number. Your function returns a string of this number with proper
US/UK comma formatting. There is a comma after every third digit in the whole number part. There
are no commas at all in the fractional part: The proper comma formatting of 1234.5678 is 1,234.5678
and not 1,234.567,8.
These Python assert statements stop the program if their condition is False. Copy them to
the bottom of your solution program. Your solution is correct if the following assert statements’
conditions are all True:
assert commaFormat(1) == '1'
assert commaFormat(10) == '10'
assert commaFormat(100) == '100'
assert commaFormat(1000) == '1,000'
assert commaFormat(10000) == '10,000'
assert commaFormat(100000) == '100,000'
assert commaFormat(1000000) == '1,000,000'
assert commaFormat(1234567890) == '1,234,567,890'
assert commaFormat(1000.123456) == '1,000.123456'
Try to write a solution based on the information in this description. If you still have trouble
solving this exercise, read the Solution Design and Special Cases and Gotchas sections for
additional hints.
Prerequisite concepts: strings, str(), in operator, index(), slices, string concatenation