thenewboston-blockchain / thenewboston-python

Python library for thenewboston digital currency.
https://thenewboston.com
MIT License
104 stars 35 forks source link

Decimals #56

Open buckyroberts opened 3 years ago

buckyroberts commented 3 years ago

We would like to store the balances as integers (as we already do) however when displaying values to the users we will display them as decimals. This allows us to avoid floating point errors while keeping the values easy to work with from the users perspective.

When migrating current account balances over to the new system, we will multiple all coin balances by 100,000,000. We will initially be displaying up to 4 decimals in our apps however we would like to reserve the last 4 decimals if needed later on (when we scale).

# global constants
RESERVED_DECIMALS = 4
DENOMINATOR = 10 ** RESERVED_DECIMALS

# validate transaction amount
bit_amount = 20_000
is_valid = bit_amount % DENOMINATOR == 0

This ensures that the reserved decimals remain untouched.

bit_amount = 20_000  # is_valid would be True
bit_amount = 20_001  # is_valid would be False

Digits (1)

buckyroberts commented 3 years ago

Note to self - reserving decimals will lead to early inaccurate calculations, different results for the same calculations for earlier adopters and possibly migration/forking issues later. Instead of reserving decimals, we most likely want to just multiple all balances by 100,000,000 to convert values to bits and then just round on the UI (configurable, they can see full values if needed).