skip to Main Content

I want to add or insert thousands commas in between digits of a number as long as I input digits like some calculators (see the image). For example, when I begin inputting the digits 793, the code inserts automatically a thousands comma if I input the forth digit say for example 5, hense the number looks like 7,935 and so forth. I have no idea, it seems difficult for me. You can show me with a variable, a text edit or anything else, and any help even small would be appreciated.

[enter image description here](https://phpout.com/wp-content/uploads/2023/08/rRR07-jpg.webp

2

Answers


  1. You can format number on display like:

    print(f"{your_number:,}")
    
    Login or Signup to reply.
  2. You can use Python’s built-in string formatting to achieve this. Here’s how you can insert thousands commas into a number.

    def format_number_with_commas(number):
        return "{:,}".format(number)
    
    # Test
    n = 1234567890
    formatted_number = format_number_with_commas(n)
    print(formatted_number)  # Output: 1,234,567,890
    

    When you run the code above, it will print 1,234,567,890. The format() function with the :, format specifier adds commas as thousands separators.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search