arantebw
arantebw

Reputation: 93

String format specifier handling float variable

def to_celsius(self):
     """Fahrenheit to Celsius"""

     fah = float(self.ent_fahrenheit.get())
     cel = (5 / 9) * (fah - 32)

     tkMessageBox.showinfo("Fahrenheit To Celsius",
             "%s deg F is equal to %.2f deg C" % (fah, cel))

I have a Python 2 code snippet using Tkinter to display a simple message box.

I am wondering if it is okay that the format specifier %s is handling (receiving) a float value from variable fah?

It seems like it is because I am not encountering any error of some sort when I ran the program.

Upvotes: 0

Views: 54

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799240

Using %s is fine, but is still the equivalent of calling str() on the object. If you want to be able to specify float width and precision then you should use one of the float specifiers (e, E, f, F, g, G) instead.

Upvotes: 1

Related Questions