473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to format a numeric Entry with commas (,) in Python

Am using Python 3.7 when a user enters a number, he expects to see the figure displayed in the entry field with commas.

eg 3755125.70 to be displayed as 3,755,125.70. Is it possible?
Mar 5 '20 #1
5 3659
SioSio
272 256MB
Expand|Select|Wrap|Line Numbers
  1. f = float(input())
  2. print('{:,}'.format(f))
Mar 6 '20 #2
Please is it possible to be part of the Entry options python?
eg FloatNumber=Entry(textvariable = f, ('{:,}'.format(f))

thanks
May 5 '20 #3
Ishan Shah
47 32bit
For your solution, the following code will work :

Expand|Select|Wrap|Line Numbers
  1. from tkinter import *
  2. import locale
  3. root = Tk()
  4.  
  5. FloatNumber = float(input('Enter no : '))
  6. b = Entry(root, textvariable = FloatNumber, justify = RIGHT).pack()
  7.  
  8. def getOutput(*events):
  9.     x = FloatNumber.get()
  10.     y = x.replace(",",'')
  11.     z = float(y)
  12.     print(z)
  13.     try:
  14.         asd = format(z,',')
  15.         print(asd)
  16.     except:
  17.         print("b")
  18.     #Overwrite the Entrybox content using the widget's own methods
  19.     b.delete(0, END)
  20.     b.insert(0, asd)
  21.  
  22. FloatNumber.trace('w',getOutput)
  23. root.mainloop()
  24.  
May 8 '20 #4
import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US')
'en_US'
>>> locale.format("%d", 3755125, grouping=True)
'3,755,125'
May 8 '20 #5
In this method, the part after the colon is the format specifier. The comma is the separator character you want,
This is equivalent of using format(num, ",d") for older versions of python.
we can use my_string = '{:,. 2f}'. format(my_number) to convert float value into commas as thousands separators.
May 9 '20 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jive | last post by:
Here's my sitch: I use gnuplot.py at work, platform Win32. I want to upgrade to Python 2.4. Gnuplot.py uses extension module Numeric. Numeric is now "unsupported." The documentation says "If...
0
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade...
3
by: Mark | last post by:
I have a Decimal or a Double - your choice. I'd like to print the number as string with a specified number of decimal places. Let's say my number 110.5, and I'd like to capture it in a string...
4
by: Mike Moore | last post by:
We would like to use a validator to valid that only numeric data is allowed in a text box. Is there a way to do this? If so, how?
0
by: Dustan | last post by:
Earlier I asked about limitting what a user can enter in an Entry. I got a few greatly appreciated replies, but I was hoping it would be possible to take a more direct approach, that is, is there a...
2
by: Netmonster | last post by:
All, How to format string add commas at thousands position, and hundred thousands position Example 1: I have string s = "1001"; or could be a random number I want the output to be 1,001. ...
2
by: JP SIngh | last post by:
Hi All We are creating a multi-region ASP application which will be using SQL Server 2000. As our users exist in multiple location i.e. UK, US, Australia how can we distinguish that the date...
8
by: libsfan01 | last post by:
how can i use regular expressions to ensure a mysql format date entry in a text field? thanks marc
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
2
by: Earl G via AccessMonster.com | last post by:
I have tried. But now I ask. I cannot get a control to accept a numeric entry (e.g. 0.23) without first entering a zero. I want to allow only two decimal places. But I would like numbers less...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.