I am building a calculator with BSP. As I tested it with various numbers, I ran into a problem that the decimal numbers don't display correctly.
For example. 58.85 -> 58.849999. But 58.84 or 58.86 work just fine. 58.8471 -> 54.84710000000001. At the end last typed digit will be saved out of nowhere.
My code following below.
method GENERATE_NUM.
DATA: lv_digi type I. * number of digits after the decimal point
call METHOD me->get_decimal
RECEIVING
getdigits = lv_digi.
*if it is a natural number
IF lv_digi = 0.
IF thisnum < 0.
result = thisnum * 10 - newdigit.
ELSE.
result = thisnum * 10 + newdigit.
ENDIF.
*if it is a float number
Else.
IF thisnum < 0.
result = thisnum - ( newdigit / 10 ** lv_digi ).
ELSE.
result = thisnum + ( newdigit / 10 ** lv_digi ).
ENDIF.
*increase the number of decimal point by 1
call method me->set_decimal.
ENDif.
endmethod.
What I basically do is everytime a number is clicked, it calls the "generate_num" method.
It takes THISNUM, NEWDIGIT, RESULT as parameters.
thisnum = current number (eg:58.8)
newdigit = clicked number (eg: 5)
result = generated number (expected: 58.85 but returns 58.849999).