This is more like a design problem than technical, of maintaining currency amounts in my web application.
The scenario is – I am bringing some currency amount in a html form. The form is organised as a table of rows with few amounts as read only and others to be filled in by user. After proposing starting estimates, I have to update read only fields based on user input. The amounts on UI are number showing decimal point between dollars and cents but on backend these amounts are being calculated as integers.
Here is am example, if 2 numbers 3.33 and 6.60 are to be added and sum is to be displayed.
These 2 numbers are first changed to 333 and 660, then added and then converted to 9.93 using text manipulations.
Can someone please propose a design level solution for this problem.
Floats are a poor choice for currency because they cannot accurately represent all decimal values without rounding. Better to use BigDecimal.
I’m not sure why you need integers on the backend to correctly do calculations with decimal currency values, but you have your choice of either rounding the numbers, or just taking their integer parts (truncating the decimals).