Transaction precision
Ensure correctness when recording amounts with floating points in your Blnk Ledger
In Blnk, balances are stored as integers, while transaction amounts are accepted as float. To correctly compute your balance, transaction amounts have to be stored as integers as well. This ensures that your ledger is accurate.
What is precision?
Blnk uses precision to convert your transaction amounts into the lowest unit possible for their asset class, e.g. converting Dollar to cents.
Let’s dive in ✨
What we’ll cover …
1. Why use precision
Precision is crucial because computers are not great at storing and managing floating points as best as Blnk needs it to. Learn more: Floating Point Math.
For instance, if a customer sends 1000.50 to a vendor, Blnk accepts the amount value as 1000.50 (float), retrieves the defined precision (e.g. 100), and applies it to the amount. This converts our amount from 1000.50 to 100050.
This ensures that all amounts are accounted for when computing the balances, and we can accurately store amounts without dealing with the technical issues of managing floating points.
2. Applying precision
To set precision on your transaction, the precision
field must be passed while recording a transaction. The default value, if not passed, is set to 1.
Consider the following sample request:
Determining your precision value
When recording a transaction with a particular asset class (e.g., USD), determine the lowest possible amount and choose a precision value that ensures that the amount is converted to the lowest unit possible.
Sample asset class | Lowest possible amount | Suggested precision value | Lowest unit to be stored |
---|---|---|---|
GBP | 0.01 | 100 | 1 |
USD | 0.01 | 100 | 1 |
BTC | 0.00000001 | 100000000 | 1 |
ETH | 0.0000000000000000001 | 1000000000000000000 | 1 |
We created an open source repository of all 151 fiat currencies and their respective precision values. See here:
Here’s the response from our request earlier:
We save the provided amount
in float and calculate the precise_amount
using the provided precision value (amount
* precision
). We use the precise_amount
to compute your ledger balances.
When retrieving balances to display in your application, you can always convert back to the primary asset class by dividing with the precision
value.
3. Best practices
-
Consistency: It is very crucial to maintain consistency in how precision is applied in your application.
- Transactions with the same
currency
should have the same precision applied to its amounts across your application. - Ensure that your precision value converts the target amount to the lowest unit possible. For example, while “100” converts most fiat currencies to their lowest unit, crypto currencies like Bitcoin can have up to “100000000” as their precision value.
- Transactions with the same
-
Review and audit: Regularly review and audit you ledger to ensure that precision is applied consistently and correctly.
-
Ensure correctness: When performing transactions between balances of two different asset classes, e.g., USD and GBP, make sure they have the same precision values.
It is not advisable to record transactions between balances with two currencies of different precision values, e.g., transactions between a USD balance and a BTC balance.
This can complicate your financial operations, cause inconsistencies in your ledger, and lead to errors in computing your balances.
Why consistency matters
-
Accuracy: Consistent use of precision ensures that all transaction amounts and balances are accurately represented in your Blnk Ledger, preventing calculation errors, where applicable.
-
Comparability: Transactions and balances with the same precision are directly comparable, simplifying financial analysis and reporting.
-
Integrity: Maintaining a uniform approach to precision preserves the integrity of your Blnk Ledger, making it more reliable and trustworthy for other financial operations.
Need help?
We are very happy to help you make the most of Blnk, regardless of whether it is your first time or you are switching from another tool.
To ask questions or discuss issues, please join our Discord community.
Manage your Blnk Ledger and explore advanced features (access control & collaboration, anomaly detection, secure storage & file management, etc.) in one dashboard.
Was this page helpful?