You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calculate the token count from the token balance of an account address. (token count *may* be a fraction)
348
+
Calculate the token count from the token balance of an account address.
349
349
350
350
.. code-block:: python
351
+
352
+
from decimal import Decimal
351
353
352
-
count = balance / (10** decimals)
354
+
# Most applications expect *exact* results, so using the Decimal library,
355
+
# with default precision of 28, is usually sufficient to avoid any rounding error.
356
+
count = balance / Decimal(10** decimals)
353
357
# -> 1
354
358
359
+
Balance is *always* an integer in the currency's smallest natural unit (i.e. `wei` for ether). Token balance is typically used only for backend calculations.
360
+
361
+
Token count *may* be a integer or fraction in the currency's primary unit (i.e. `eth` for ether). Token count is typically displayed to the user on the front-end since it is more readable.
0 commit comments