I am creating a calculator with BSP. When I tested it with different numbers, I ran into the problem that decimal numbers are not displayed correctly.
For instance. 58.85 → 58.849999. But 58.84 or 58.86 work fine. 58.8471 → 54.84710000000001. At the end, the last digit entered will be saved from nowhere.
My following code is 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 every time a number is clicked, it calls the generate_num method. It takes THISNUM, NEWDIGIT, RESULT as parameters.
thisnum = current number (ex: 58.8)
newdigit = number of pressed (ex: 5)
result = generated number (expected: 58.85, but returns 58.849999).