Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting floats
#7
(Oct-04-2018, 04:16 PM)gruntfutuk Wrote: BAD:
print(end='In 1 year, the tuition will be $' + str(totalOnePrice) + '.\n')

BETTER:
print(end='In 1 year, the tuition will be $' + '{:.12f}'.format(totalOnePrice) + '.\n')

but using end=<str> in such a way is, imho, a very bad practice.

PS. Using floats when dealing with money is not usually a good idea. Work in cents or use the decimal package.

BEST:
print('In 1 year, the tuition will be ${:.12f}.\n'.format(totalOnePrice))
end seems somehow redundant.

PS I know, f-formatting
print(f'In 1 year, the tuition will be ${totalOnePrice:.12f}.\n')
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
Formatting floats - by Irhcsa - Oct-04-2018, 03:49 PM
RE: Formatting floats - by volcano63 - Oct-04-2018, 04:01 PM
RE: Formatting floats - by gruntfutuk - Oct-04-2018, 04:04 PM
RE: Formatting floats - by volcano63 - Oct-04-2018, 04:10 PM
RE: Formatting floats - by Irhcsa - Oct-04-2018, 04:05 PM
RE: Formatting floats - by gruntfutuk - Oct-04-2018, 04:16 PM
RE: Formatting floats - by volcano63 - Oct-04-2018, 04:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  When is it safe to compare (==) two floats? Radical 4 746 Nov-12-2023, 11:53 AM
Last Post: PyDan
  floats 2 decimals rwahdan 3 1,644 Dec-19-2021, 10:30 PM
Last Post: snippsat
  rounding and floats Than999 2 3,127 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  int, floats, eval menator01 2 2,454 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 3,055 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  rounding floats to a number of bits Skaperen 2 2,324 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  comparing fractional parts of floats Skaperen 4 3,380 Mar-19-2019, 03:19 AM
Last Post: casevh
  Integer vs Floats Tazbo 2 2,887 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  How do you sort a table by one column?? (of floats) sortedfunctionfails 3 12,319 Jan-11-2018, 09:04 AM
Last Post: sortedfunctionfails
  Reading floats and ints from csv-like file Krookroo 15 20,114 Sep-05-2017, 03:58 PM
Last Post: Krookroo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020