+ 1
Can anyone improve my code for efficiency or suggest any other fast way than mine
Write a function named format_number that takes a non-negative number as its only parameter. Your function should convert the number to a string and add commas as a thousands separator. For example, calling format_number(1000000) should return "1,000,000". https://code.sololearn.com/cn1Q8cwt0qIN/?ref=app
2 Antworten
+ 4
# Here are some ways to do it with formatting
num = 123456789.4598
s = f'{num:,}' # F-Strings
s = '{:,}'.format(num) # String Formatting
s = format(num, ',') # Built-in Format Function
print(s)
+ 4
num = 123456789.45
print(f"{num:,}")
# Keep learning & happy coding :D