DEV Community

Cover image for How are you handling your money?
Olaniyi Philip Ojeyinka
Olaniyi Philip Ojeyinka

Posted on

How are you handling your money?

When working on Financial Software, one have to be extremely careful of how things are done from planning, implementation, Platform security to basic arithmetics.
Back to the topic, we will be discussing money handling with great precision, so you've been tasked to create a micro payment transfer application maybe for fiat or crypto.
Yeah for the inexperienced, this is quiet simple, just create db schema, define the balance column data types as decimal and start storing, retrieving and manipulating the data as needed using the float datatype or equivalent in your chosen language.

You've just shipped your software, and there comes John who have the balance of $0.8 and will like to transfer 0.5 to Doe another user of the software, Yeah this should be pretty straight forward right? we get the balance of John then subtract 0.5 from it, update John balance with result then add 5.0 to Doe's balance. It will have been this easy, if generally computers are great with floating point calculation without loosing precision but in reality, John maybe getting more money than he is supposed to get, assuming the datatype of the balance column isn't constraint by 2 decimal places or even when the result of the calculations is needed to be sent to a third party service like a payment gateway.

Another reason this is important to prepare for at the planning stage of your finance software development is that, its not uncommon to compare two or more values. When we get to a situation where we need to compare our values, we need to make sure the result of our calculations equal to expected value.
With JavaScript,

0.8 - 0.5 = 0.30000000000000004 
// normally, we would have expected to get just 3.0

Enter fullscreen mode Exit fullscreen mode

In the scenario described above, we will be introducing unexpected behaviour to the software if the result does not equal to what we normally would have expected.

 0.30000000000000004 === 0.3
 false

Enter fullscreen mode Exit fullscreen mode

This is not a Javascript problem as most memes you probably would have seen may have projected it, its common among other programming languages like Ruby, Java, Golang and In fact, its generally an issue with any computer systems that follows IEEE_754 and because computers process its operations in binary(low level stage) and floating point been a recurring number, at some point, we will run out of bit to represent the float depending on the processor's architecture whether 64bit or 32bit(The higher the bits, the more the precision) resulting to floating point rounding error.

So how do we get our way around this in our financial software, one approach is to represent money in the lowest unit, for example in case of Dollar, you can process money in cent, perform all needed calculations in its lowest unit and only return back to 100 whenever you need to render it to the user. Fiat generally are represented in two decimal places, so they can be reprecented by the hundredth.

In case of crypto, this will also depend on the lowest unit of the crypto for bitcoin which is 8 decimal places which means its lowest unit is 0.00000001 called satoshi, we can represent it in 100,000,000th. How do you prepare your schema in this case? you can have currencies table/document with fields as follows

Name
Code
lowest_unit
lowest_unit_name

where in a case of dollar, the data will be as follows:

US Dollar
USD
100
cent

Then whenever you need to render an amount, you divide by lowest_unit and round to the needed precision, at the point you are collecting the value as input from user or third party, you multiply by lowest_unit.

Another different approach maybe to have the money represented in the decimal in your database at the decimal places of the currency with the highest decimal point you are going to be dealing with for example using Decimal(n, 2) in case of fiat, and perform your calculations in arbitrary-precision Decimal type.
In javascript, using libraries like Big.js , Decimal.js will be of help.

In Java and Ruby, BigDecimal is an inbuilt class that can be used.
For PHP, you can checkout the Brick Math Library
Basically, just find the arbitrary-precision Decimal type/Class/Library in your choice of language.
Got another approach, please do let me know in comment section.

Top comments (3)

Collapse
 
gude1 profile image
Gideon Iyinoluwa Owolabi

Nice thread sir. Well done

Collapse
 
niyiojeyinka profile image
Olaniyi Philip Ojeyinka

🙌

Collapse
 
gude1 profile image
Gideon Iyinoluwa Owolabi

Nice thread👏