Formatting floats - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Formatting floats (/thread-13221.html) |
Formatting floats - Irhcsa - Oct-04-2018 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 RE: Formatting floats - volcano63 - Oct-04-2018 (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. 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 Someone will tel you about formatting post, but if you want to develop Python - I strongly suggest to follow PEP-8 conventions
RE: Formatting floats - gruntfutuk - Oct-04-2018 (Oct-04-2018, 04:01 PM)volcano63 Wrote: The moment you applied Illegal! Wow. Didn't think learning to programme was that serious.
RE: Formatting floats - Irhcsa - Oct-04-2018 (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. Then how would I do it? I have to make it produce a float to the 12 decimal. Is it not possible? RE: Formatting floats - volcano63 - Oct-04-2018 (Oct-04-2018, 04:04 PM)gruntfutuk Wrote:(Oct-04-2018, 04:01 PM)volcano63 Wrote: The moment you appliedIllegal! Wow. Didn't think learning to programme was that serious. exception == illegal (by Python laws, of course, not by Her Majesty's )(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 . Take a look again at my example PS You need a float value to format it as a float. str(value) creates a string
RE: Formatting floats - gruntfutuk - Oct-04-2018 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. RE: Formatting floats - volcano63 - Oct-04-2018 (Oct-04-2018, 04:16 PM)gruntfutuk Wrote: BAD: BEST: print('In 1 year, the tuition will be ${:.12f}.\n'.format(totalOnePrice)) end seems somehow redundant.PS I know, f -formattingprint(f'In 1 year, the tuition will be ${totalOnePrice:.12f}.\n') |