I’m trying to get 2 decimal places of 0s (e.g, 9.00) in the budget app project. I tried using string formatting, but the program expects a floating point number, not a string. Casting it to a float results in a single .0 because that’s how floating point numbers work in Python - that gets rejected, the program wants two 0s. I tried using the Decimal library and writing
but I’m getting an error stating that “rounding is not a valid key argument for this function (quantize)”. I was pretty sure that it was, but there you go.
I’m really stuck on how to go about this. If the syntax in the documentation of Decimal is being rejected as invalid syntax, I am not sure how to use Decimal. Is there another way to go about this, if I can’t write it as a float and I can’t use Decimal?
But none of those suggestions answer my question. Most of them return an integer, one returns a string, and round(amount, 2) returns a float that defaults to a single 0. I did actually Google this myself. Didn’t find an answer other than Decimal, which as I said, doesn’t seem to be working for reasons I am not sure about.
That is indeed a practical way to handle currency, unfortunately it doesn’t help in the budget app project as, in order to pass, you need to be able to return a floating point number with two decimal places that are both 0s
Yes, but since I can’t return it as a float (which would default to a single 0), I am not sure what I can return it as. String won’t work as the program is expecting a number. Do you have any ideas?
Which method specifically are you working on? The only place that wants two zeros is the string outputs? Isn’t it? For those I just used formatted strings.
def deposit(self, amount:float, description = ""):
short_descript = ""
if(len(description)>22):
for i in range(0, 23):
short_descript+=description[i]
Decimal(amount).quantize(Decimal(0.00), rounding=ROUND_FLOOR)
self.ledger.append({"amount": amount, "description": description})
self.funds+=amount
Which, when passed either an integer amount or a float has to return a floating point number with two decimal places. It works fine if it is passed a float, but if passed an integer it doesn’t create two decimal places under “amount”. I tried round(amount, 2) but that defaults to a single 0, because floating point numbers in Python always do. I suspect I need a special library, which is probably the Decimal library, but in VS Code it throws this error about “rounding” not being a method of the function. In Replit, nothing happens at all, good or bad. I suspect the ROUND_FLOOR may be wrong, I can try a different rounding method. I just assumed FLOOR since I am “rounding” to the lowest number possible, just in case Decimal tries to stick extra numbers on the end because of inherent problems with floating point inn programming languages.
I just used replit for this sample code and it worked there.
If you’re on a standalone system, try the Atom editor as it is very simple (just cut and paste the code in and test it, then hopefully when it works, you can compare line-by-line to your method to see what is different).
I store numbers so I can still do calculations with them.
The only place where two decimal points are required is when you display the values…
A list of the items in the ledger. Each line should show the description and amount. The first 23 characters of the description should be displayed, then the amount. The amount should be right aligned, contain two decimal places, and display a maximum of 7 characters.
This is the only place where I see two decimals, in the formatted view
{:.2f} is a string value. I can’t keep arguing this… I need to be able to actually display a number with 2 .0s. I tried {:.2f} but it complains that I have printed a string, not a number.