I'm attempting to build a very simple function to convert units to/from a 'Base Unit of Measurement' (ie: various different units to/from KGs), but I'm running into some inconsistent results. When I convert a value to the Base UOM I get the expected value; however, when I convert it back, I'm getting a result that does not match my expectations.
I'm currently using the decimal (numeric) data type, on my Postgres database, but if I convert the quantity column to float, the outcome is inline with what I'm expecting.
Migrations
conversions_table
$table->decimal('lb', 18, 16, true);
// value = 0.4535923700000000
consumtion_table
$table->decimal('quantity', 16, 10);
Code
// Convert LBs to KG
$quantity * $conversions->lb
// Convert KGs to LBs
$quantity / $conversions->lb
Example
If a $value = 0.125 is converted to KGs it results in the function returning 0.0566990463 KGs; therefore, when I run the function in the reverse direction to return the value in KGs back to LBs, it results in 0.12500000011023113
But, if I change the data type to float(), that same function returns 0.05669904625 KGs, which then returns the expected result when converting back to LBs -> 0.125.
Summary
I'm sure this is a 'newbie' mistake and perhaps expected behaviour due to a misunderstanding of these different data types, but I'd be very thankful if someone could clarify what the best approach is tackling this kind of scenario!