Floating point numbers to two decimal places - possible with standard lib?

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

Decimal(amount).quantize(Decimal(0.00), rounding=ROUND_FLOOR)

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?

A quick google of your question gives this:

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.

If you are staying in the same currency system, I recommend keeping track of actual cents instead of dollars.

The following code sample works for me in terms of no errors are given about the quantize function. Take a look:

import decimal
from decimal import Decimal
a = Decimal(amount).quantize(Decimal(0.00), rounding=decimal.ROUND_FLOOR)

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

I’m not sure why mine is throwing an error then. I’ll do some fiddling and see if there is a problem with the text editor

You can store the data in one format and return it in a different format.

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.

The deposit method. So far I have

def deposit(self, amount:float,  description = ""):
        short_descript = ""
            for i in range(0, 23):
        Decimal(amount).quantize(Decimal(0.00), rounding=ROUND_FLOOR)  
        self.ledger.append({"amount": amount, "description": description})

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.

If you want to force an integer to a decimal, you can multiply by 1.0.

No, that will not give me two 0s. Only 1. Please look up the behavior of floating point numbers in Python.

No… It will give you a decimal number that you can format to as many zeros as you want. I’m looking at my solution right now…

You are mixing up two issues and chasing the wrong problem. You need to fix how your amount is formatted for display.

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 can format it for display by using an f string, but that will give me a string. Are you getting a string or a number in your solution?

Thanks, I’ll give it a go

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.

You just are not right… I’m looking at a fully passing solution right now

I don’t know how much clearer I can be.

This would be much easier if you showed your code and error messages. You are spinning your wheels chasing the completely wrong root cause.

1 Like