0

Using the lib Decimal i can set the quantize to get the 2 decimal places with zero:

from decimal import Decimal
value = Decimal('1000.0').quantize(Decimal('1.00'))
print(value) # Decimal('1000.00')

But if i need to change it to float the last zero disappear:

value = float(value)
print(value) # 1000.0

How can i set two zeros at decimal place and still use as float?

the print method was an e example to show the final result, i need to use the value as float and not string.

the value will be used in a json request:

{
    "value": 1000.00
}
Rafa Acioly
  • 530
  • 9
  • 34
  • 1
    Possible duplicate of [Display a float with two decimal places in Python](https://stackoverflow.com/questions/6149006/display-a-float-with-two-decimal-places-in-python) – Cray May 07 '19 at 16:19
  • @Cray my question is how to get the value as float with two zeros on decimal places and not how to convert it to string, i need that the final value should be float with two zeros and not a string – Rafa Acioly May 07 '19 at 16:23
  • If you want to print it with two decimal places, don't convert it to float. What are you trying to accomplish exactly? Do you need two decimal places of precision? – wjandrea May 07 '19 at 16:23
  • @wjandrea i'm consuming an API and this api is having trouble to interpret this `1000.0`, they say that i need to send as `1000.00` (json) – Rafa Acioly May 07 '19 at 16:25
  • @Rafa I just saw your comment above. What you're asking for (float `__str__`/`__repr__` showing two decimal places) is not possible, unless you subclass `float` or convert it to a string. Please make a [mcve] and explain exactly what you need. – wjandrea May 07 '19 at 16:26
  • OK, just saw your edit. Yes, you actually do want to convert it to a string. Though I'm not sure how exactly to convert that into json. Maybe this? [Format floats with standard json module](https://stackoverflow.com/q/1447287/4518341) Edit: actually I'm not sure - sorry, I have little experience in this. – wjandrea May 07 '19 at 16:29
  • 1
    Actually, you do not care if it is `1000` or `1000.00` in a json, because this is the same thing – BlueSheepToken May 07 '19 at 16:33

0 Answers0