Fix 3.3 + 0.15 = 3.4499999999999997

main
Your Name 2 months ago
parent 5e54904a1c
commit 1d403e2495

@ -1,6 +1,13 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
# Fix 3.3 + 0.15 = 3.4499999999999997
from decimal import Decimal as d
def fix_add(one, two):
return float(d(str(one)) + d(str(two)))
def fix_sub(one, two):
return float(d(str(one)) - d(str(two)))
from db import *
app = FastAPI()
@ -111,7 +118,7 @@ def coins_add(it: Coins_add):
token, id, amount = it.token, it.id, float(it.amount)
if token_check(token):
db = read()
db['id'][id]['bal'] += amount
db['id'][id]['bal'] = fix_add(db['id'][id]['bal'], amount)
write(db)
return 'OK'
else:
@ -126,7 +133,7 @@ def coins_del(it: Coins_del):
token, id, amount = it.token, it.id, float(it.amount)
if token_check(token):
db = read()
db['id'][id]['bal'] -= amount
db['id'][id]['bal'] = fix_sub(db['id'][id]['bal'], amount)
write(db)
return 'OK'
else:
@ -144,9 +151,10 @@ def coins_transfer(it: Coins_transfer):
db = read()
amount = abs(amount) # Защита от отриц. чисел
src_bal = db['id'][src_id]['bal']
if src_bal > amount and amount > 0.0001:
db['id'][src_id]['bal'] -= amount
db['id'][dst_id]['bal'] += amount
# Больше баланса и количество цифр после запятой <= 3
if src_bal >= amount and len(str(amount).split('.')[1]) <= 3: # and amount > 0.0001:
db['id'][src_id]['bal'] = fix_sub(db['id'][src_id]['bal'], amount)
db['id'][dst_id]['bal'] = fix_add(db['id'][dst_id]['bal'], amount)
write(db)
return 'OK'
else:
@ -330,4 +338,4 @@ def get_passwd(it: Get_passwd):
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='127.0.0.1', port=7001)
uvicorn.run(app, host='0.0.0.0', port=7001)

Loading…
Cancel
Save