Home > database >  python decimal place changes after 0
python decimal place changes after 0

Time:07-07

hello, i am new one in programming i am sending a request from postman with payload

{"member_id":10,"value": 30.00}

but when i receiving it in api by using request.data

{'member_id': 10, 'value': 30.0}

the decimal place changes to 1 position after zero but i need same decimal values as sending in request which i need to verify signature.

thanks

CodePudding user response:

the value should be the same because what changes is just the printing format. If you need to print the number as a string with 2 decimals, you can use

value = 30.0
formated_value = '{0:.2f}'.format(value)

CodePudding user response:

It's the same thing:

>>> price = 30.00
>>> print(price)
30.0
>>> price == 30.00
True
>>>

If you want the format to be retained you can use an f string command:

f'{price:.2f}'
'30.00'
>>>

If you're worried about transferring numeric values in JSON then you could use strings to eliminate any loss of precision.

This might give some more background.

  • Related