Ghost decimal fraction

Post Reply
hidroxid
Posts: 28
Joined: Sun Apr 24, 2011 12:50 am

Ghost decimal fraction

Post by hidroxid »

Hello,

In an accounting system, a client is entering a large number of values ​​and in some of them, "ghost" decimal fractions appear which alter the totalization of the values.

In an accounting system this is not feasible.

I have several isolated amounts, but the one I take as an example is:

9747224374.80

Obviously in a single sum and with only that value, the result is not altered, but when I execute the totalization with different values ​​that have those "ghost" decimals, the differences appear.

If the value is shown with only 2 decimal places, the decimal portion is kept at .80, but if I show more than 2 decimal places (to see what there are), the .80 is changed to .799999 and that fraction accumulated with other values ​​and others Phantom fractions, they have me crazy.

I have tried to take out the decimal part and then join it with the integer and nothing.
I have tried to convert it to character, cut off the decimals, convert it back to numeric and nothing.

I did a small example in Harbor to visualize the problem to see if anyone has any clues.

Thanks in advance!

Code: Select all

#define CRLF Chr(13)+Chr(10)
#define LF Chr(10)

#define N_ENTEROS 8
#define N_DECIMALES 8

/*----------------------------------------------------------------------------*/

FUNCTION MAIN()
LOCAL cPic1, cShow, nValue0, nValue1, nValue2, nValue3, nValue4, nValue5, nValue6, nValue7, nValue8

cls

cPic1 := "999,999,999,999." + Repli( "9", N_DECIMALES )

nValue0 := 9747224374.80

// Intento separar el entero de la parte decimal y luego la añado manualmente
nValue1 := Int( nValue0 ) + 0.80

// Intento convertir a caracter con 2 decimales para luego convertirlo de nuevo a numérico
nValue2 := Str( nValue0, N_ENTEROS + N_DECIMALES + 4, 2 )
nValue2 := Val( nValue2 )

// Valores grandes sin problemas
nValue4 := 1234567890.80

nValue5 := 7597536545.80

nValue6 := 8588899999.80

nValue7 := nValue6 + 1034592

// Al sumar 1 aparece el fantasma
nValue8 := nValue7 + 1

cShow := "Original: " + Str( nValue0 ) + Space( 14 ) + LF + ;
         "Picture: " + cPic1 + LF + ;
         "Valor 1: " + Transform( nValue1, cPic1 ) + LF + ;
         "Valor 2: " + Transform( nValue2, cPic1 ) + LF + ;
         LF + ;
         "** Otros numeros grandes sin problemas **" + LF + ;
         ;
         "Valor 4: " + Transform( nValue4, cPic1 ) + LF + ;
         "Valor 5: " + Transform( nValue5, cPic1 ) + LF + ;
         "Valor 6: " + Transform( nValue6, cPic1 ) + LF + ;
         "Valor 7: " + Transform( nValue7, cPic1 ) + LF + ;
         ;
         LF + ;
         "** El anterior mas 1 y aparece el problema **" + LF + ;
         "Valor 8: " + Transform( nValue8, cPic1 )

Alert( cShow )

RETURN NIL
Resultado: https://ibb.co/64Sffc7

Image
Post Reply