Float value conversion

Hi All,

  1. I am connecting to AWS redShift DB and fetching records, while I am doing so I use cursor.fetchall() to get list of rows and construct to a Dictionary.
  2. while i do a fetch from RedShift I get decimal values as Decimal(‘1328.10’) since its not if readable format I try to convert it to float while converting to float the zero value is skipped
    3.val=float(Decimal(‘1328.10’))
    output=1328.1// The zero value is skipped is there any such to retain the value, I don’t want to use the .2f reason I want the result in a numeric not a string.

It would be a great help if you can tell some ideas?

I’m afraid you’re stuck without it.

A trailing zero (after the radix point) is meaningless to float types etc. Otherwise an arbitrary number of them could be appended after any float with a finite representation.

Optional trailing zeroes are only meaningful in string representations of floats etc.

I work for a financial institute so we have a meaning to it, saying 1328.1(1Cent) 1328.10(10cents) hope you get it

Leave it as a Decimal then. That’s what Decimal types are for. I think you have to set the context to 2 d.p.

Or alternatively, like much of the rest of the financial industry’s coders, measure the amount in cents or pennies and use an int instead, and just divide by 100 or whatever, whenever you need to display it.

I am applying the business logic and converting to JSON as an ouput for end user its not in a readable format {‘amount’:Decimal(‘3214.10’)}

Business logic is fine, but you will incorrectly implement your requirement: 1328.1(1Cent) 1328.10(10cents) if you use floats, as the business logic requirement is different to the interpretation of those strings both in mathematics, and in all the ISO standards for floats I’ve ever heard of.

can you please suggest a solution if you aware of it.

Use an int and write some custom string processing logic.

I’m more than happy to provide further help to yourself and anyone else in the financial industry at normal market rates. It won’t take me longer than 15 minutes.

the output should be a numeric not a string

No problem. Send me a copy of your company’s standard contract, and I’ll have it solved very quickly.

Are you saying your accounting system considers 0.1 and 0.10 to be distinct values? That seems… strange, to say the least. No computer implements this system. Simply put, your requirement (1328.1 and 1328.10 as distinct numerics) cannot be met.

1 Like