Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting floats
#1
For year 5 I need it to format to the 12th decimal place. I have tried reading the documentation, but it isn't working.
I need str(yearFiveTest) to express to 12 decimals. I have tried %.12f like the documentation states. I have tried .format(.12f)

Can anyone help please?

Here is my code:

price = 8000
myLoop = True
while myLoop == True:
yearOne = price * .03
totalOnePrice = yearOne + price
print(end='In 1 year, the tuition will be $' + str(totalOnePrice) + '.\n')
yearTwo = totalOnePrice * .03
yearTwoTest = yearTwo + totalOnePrice
print(end='In 2 years, the tuition will be $' + str(yearTwoTest) + '.\n')
yearThree = yearTwoTest * .03
yearThreeTest = yearThree + yearTwoTest
print(end='In 3 years, the tuition will be $' + str(yearThreeTest) + '.\n')
yearFour = yearThreeTest * .03
yearFourTest = yearFour + yearThreeTest
print(end='In 4 years, the tuition will be $' + str(yearFourTest) + '.\n')
yearFive = yearFourTest * .03
yearFiveTest = yearFive + yearFourTest
print(end='In 5 years, the tuition will be $' + str(yearFiveTest) + '.\n')
myLoop = False
Reply
#2
(Oct-04-2018, 03:49 PM)Irhcsa Wrote: For year 5 I need it to format to the 12th decimal place. I have tried reading the documentation, but it isn't working.
I need str(yearFiveTest) to express to 12 decimals.

The moment you applied str conversion, you made formatting options irrelevant (actually, illegal). The purpose of formatting is to facilitate conversion to string according to specification

Output:
In [2]: '{:.12f}'.format(0.56456454646) Out[2]: '0.564564546460' In [3]: '{:.12f}'.format(str(0.56456454646)) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-2e1f324cded6> in <module>() ----> 1 '{:.12f}'.format(str(0.56456454646)) ValueError: Unknown format code 'f' for object of type 'str'
Someone will tel you about formatting post, but if you want to develop Python - I strongly suggest to follow PEP-8 conventions
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
#3
(Oct-04-2018, 04:01 PM)volcano63 Wrote: The moment you applied str conversion, you made formatting options irrelevant (actually, illegal)...
Illegal! Wow. Didn't think learning to programme was that serious.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#4
(Oct-04-2018, 04:01 PM)volcano63 Wrote:
(Oct-04-2018, 03:49 PM)Irhcsa Wrote: For year 5 I need it to format to the 12th decimal place. I have tried reading the documentation, but it isn't working.
I need str(yearFiveTest) to express to 12 decimals.

The moment you applied str conversion, you made formatting options irrelevant (actually, illegal). The purpose of formatting is to facilitate conversion to string according to specification

Output:
In [2]: '{:.12f}'.format(0.56456454646) Out[2]: '0.564564546460' In [3]: '{:.12f}'.format(str(0.56456454646)) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-2e1f324cded6> in <module>() ----> 1 '{:.12f}'.format(str(0.56456454646)) ValueError: Unknown format code 'f' for object of type 'str'
Someone will tel you about formatting post, but if you want to develop Python - I strongly suggest to follow PEP-8 conventions

Then how would I do it? I have to make it produce a float to the 12 decimal. Is it not possible?
Reply
#5
(Oct-04-2018, 04:04 PM)gruntfutuk Wrote:
(Oct-04-2018, 04:01 PM)volcano63 Wrote: The moment you applied str conversion, you made formatting options irrelevant (actually, illegal)...
Illegal! Wow. Didn't think learning to programme was that serious.
exception == illegal (by Python laws, of course, not by Her Majesty's Tongue )

(Oct-04-2018, 04:05 PM)Irhcsa Wrote: Then how would I do it? I have to make it produce a float to the 12 decimal. Is it not possible?

I have just shown you how Confused . Take a look again at my example

PS You need a float value to format it as a float. str(value) creates a string
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
#6
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is it safe to compare (==) two floats? Radical 4 652 Nov-12-2023, 11:53 AM
Last Post: PyDan
  floats 2 decimals rwahdan 3 1,588 Dec-19-2021, 10:30 PM
Last Post: snippsat
  rounding and floats Than999 2 3,047 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  int, floats, eval menator01 2 2,404 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 2,958 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  rounding floats to a number of bits Skaperen 2 2,272 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  comparing fractional parts of floats Skaperen 4 3,273 Mar-19-2019, 03:19 AM
Last Post: casevh
  Integer vs Floats Tazbo 2 2,843 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  How do you sort a table by one column?? (of floats) sortedfunctionfails 3 12,200 Jan-11-2018, 09:04 AM
Last Post: sortedfunctionfails
  Reading floats and ints from csv-like file Krookroo 15 19,909 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