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
(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
(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.
(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?
(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
)
(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
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.
(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')