------------------------------------------------------------------------------------------------
                      [BUG/PRB.] VFP 9.0 FIX - THE DECIMAL PLACES OF CURRENCY DATA FOR THE CALCULATE COMMAND
                                 January 2024
                 ------------------------------------------------------------------------------------------------
                                    CCB



1. BUG:

     In vfp9 (and vfp6, vfp7, vfp8), it does not set the decimal places of currency data for the CALCULATE command,
     so the decimal places of currency data (Value.ev_length) is a random number, 
     sometimes it causes the error "Feature is not available (Error 1001)".

     There is the bug for the SUM() function and the AVG() function, there is no the bug for other aggregate functions.

     In Visual FoxPro Advanced, it will set the decimal places of currency data to 4 for the CALCULATE command,
     so there is no the bug.


2. CAUSE:

     There are some BUGs in the following code.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Label4472ca ::
             cmp  byte ptr [ edi + 1 ] , 00h                                 ;0x004472ca :        807f0100
             jne Label6083d6                                                 ;0x004472ce :        0f8502111c00

     CallBack4472d4 :: ; proc near
             fld qword ptr [ edi + 48 ]                                      ;0x004472d4 :        dd4730
             mov  byte ptr [ edi + 4 ] , 04Eh                                ;0x004472d7 :        c647044e

     Label4472db ::
             fstp qword ptr [ edi + 20 ]                                     ;0x004472db :        dd5f14

     Label4472de ::
             inc ebx                                                         ;0x004472de :        43
             cmp  ebx , dword ptr [ ebp + 124 ]                              ;0x004472df :        3b5d7c
             jb Label4472ad                                                  ;0x004472e2 :        72c9

     Label4472e4 ::
             xor esi , esi                                                   ;0x004472e4 :        33f6
             cmp  dword ptr [ ebp + 112 ] , esi                              ;0x004472e6 :        397570
             mov ebx , 0200h                                                 ;0x004472e9 :        bb00020000
             jne Label608404                                                 ;0x004472ee :        0f8510111c00

     Label4472f4 ::
             cmp  dword ptr [ ebp + 124 ] , esi                              ;0x004472f4 :        39757c
             mov  dword ptr [ ebp + 080h ] , esi                             ;0x004472f7 :        89b580000000
             jbe Label44738b                                                 ;0x004472fd :        0f8688000000
             lea eax ,  dword ptr [ ebp + 0FFFFFBC4h ]                       ;0x00447303 :        8d85c4fbffff
             mov  dword ptr [ ebp + 120 ] , eax                              ;0x00447309 :        894578

     Label44730c ::
             mov  eax , dword ptr [ ebp + 080h ]                             ;0x0044730c :        8b8580000000
             mov  edi , dword ptr [ ebp + 4 * eax + 0FFFFFEC4h ]             ;0x00447312 :        8bbc85c4feffff
             lea esi ,  dword ptr [ edi + 4 ]                                ;0x00447319 :        8d7704
             mov  cl , byte ptr [esi]                                        ;0x0044731c :        8a0e
             cmp cl , 048h                                                   ;0x0044731e :        80f948
             je Label447337                                                  ;0x00447321 :        7414
             mov  ax , word ptr [ edi + 76 ]                                 ;0x00447323 :        668b474c
             cmp ax , 01FFh                                                  ;0x00447327 :        663dff01
             movzx eax , ax                                                  ;0x0044732b :        0fb7c0
             jnb Label608590                                                 ;0x0044732e :        0f835c121c00

     Label447334 ::
             mov  dword ptr [ edi + 8 ] , eax                                ;0x00447334 :        894708

     Label447337 ::
             cmp cl , 04Eh                                                   ;0x00447337 :        80f94e
             jne Label44734c                                                 ;0x0044733a :        7510
             mov eax ,  dword ptr [ Data9370f8 ]                             ;0x0044733c :        a1f8709300
             mov  eax , dword ptr [eax]                                      ;0x00447341 :        8b00
             mov  eax , dword ptr [ eax + 0D8h ]                             ;0x00447343 :        8b80d8000000
             mov  dword ptr [ edi + 12 ] , eax                               ;0x00447349 :        89470c

     Label44734c ::


     ;
     ;                 -------------------------------------------------------------------------------------
     ;                      VFP 9.0 FIX - THE DECIMAL PLACES OF CURRENCY DATA FOR THE CALCULATE COMMAND
     ;                                February 2023
     ;                 -------------------------------------------------------------------------------------
     ;                                     CCB
     ;
     ; In vfp9 (and vfp6, vfp7, vfp8), it does not set the decimal places of currency data for the CALCULATE command,
     ; so the decimal places of currency data (Value.ev_length) is a random number, 
     ; sometimes it causes the error "Feature is not available (Error 1001)".
     ; In Visual FoxPro Advanced, it will set the decimal places of currency data to 4 for the CALCULATE command,
     ; so there is no the bug.
     ;
     ; 2023/2/9, by ccb
     ;

             cmp cl , 059h
             jne Label44734d
             mov  dword ptr [ edi + 12 ] , 04h


     Label44734d ::
             cmp  word ptr [ ebp + 100 ] , 00h                               ;0x0044734c :        66837d6400
             je Label60859a                                                  ;0x00447351 :        0f8443121c00
             mov  edx , dword ptr [ ebp + 120 ]                              ;0x00447357 :        8b5578

     Label44735a ::
             mov ecx , esi                                                   ;0x0044735a :        8bce
             pushd 01h                                                       ;0x0044735c :        6a01
             call Fun52bf26                                                  ;0x0044735e :        e8c34b0e00

     Label447363 ::
             cmp  dword ptr [ ebp + 112 ] , 00h                              ;0x00447363 :        837d7000
             jne Label6085b5                                                 ;0x00447367 :        0f8548121c00

     Label44736d ::
             cmp  byte ptr [esi] , 048h                                      ;0x0044736d :        803e48
             je Label608687                                                  ;0x00447370 :        0f8411131c00

     Label447376 ::
             inc  dword ptr [ ebp + 080h ]                                   ;0x00447376 :        ff8580000000
             mov  eax , dword ptr [ ebp + 080h ]                             ;0x0044737c :        8b8580000000
             add   dword ptr [ ebp + 120 ] , 018h                            ;0x00447382 :        83457818
             cmp  eax , dword ptr [ ebp + 124 ]                              ;0x00447386 :        3b457c
             jb Label44730c                                                  ;0x00447389 :        7281

     Label44738b ::
             lea eax ,  dword ptr [ ebp + 76 ]                               ;0x0044738b :        8d454c
             push eax                                                        ;0x0044738e :        50
             lea eax ,  dword ptr [ ebp - 60 ]                               ;0x0044738f :        8d45c4
             call Fun42d200                                                  ;0x00447392 :        e8695efeff
             mov  ecx , dword ptr [ ebp + 72 ]                               ;0x00447397 :        8b4d48
             test ecx , ecx                                                  ;0x0044739a :        85c9
             jne Label608694                                                 ;0x0044739c :        0f85f2121c00

     Label4473a2 ::
             mov  ecx , dword ptr [ ebp + 68 ]                               ;0x004473a2 :        8b4d44
             test ecx , ecx                                                  ;0x004473a5 :        85c9
             jne Label60869e                                                 ;0x004473a7 :        0f85f1121c00
             lea esp ,  dword ptr [ ebp + 0FFFFFBB8h ]                       ;0x004473ad :        8da5b8fbffff
             mov  ecx , dword ptr [ ebp + 0284h ]                            ;0x004473b3 :        8b8d84020000
             call Fun42bf1d                                                  ;0x004473b9 :        e85f4bfeff
             pop edi                                                         ;0x004473be :        5f
             pop esi                                                         ;0x004473bf :        5e
             pop ebx                                                         ;0x004473c0 :        5b
             add ebp , 0288h                                                 ;0x004473c1 :        81c588020000
             leave                                                           ;0x004473c7 :        c9
             ret                                                             ;0x004473c8 :        c3


4. APPLIES TO:

     VFP 6.0.8167.0
     VFP 6.0.8961.0 (SP5)

     VFP 7.0.0.9262
     VFP 7.0.0.9465 (SP1)

     VFP 8.0.0.2521
     VFP 8.0.0.3117 (SP1)

     VFP 9.0.0.2412
     VFP 9.0.0.3504 (SP1)
     VFP 9.0.0.4611 (SP2)
     VFP 9.0.0.5015 (SP2)
     VFP 9.0.0.5411 (SP2)
     VFP 9.0.0.5721 (SP2)
     VFP 9.0.0.5815 (SP2)
     VFP 9.0.0.6303 (SP2)
     VFP 9.0.0.6602 (SP2)
     VFP 9.0.0.7423 (SP2)

     The bug has been fixed in VFP Advanced.

     IMPORTANT NOTE:
     In vfp9 (and vfp6, vfp7, vfp8), we can use the NTOM() function to set the decimal places of currency data to 4, for example,
     CALCULATE SUM(Amount) TO pnS
     pnS=NTOM(MTON(pnS))
     and then there is no the bug.


5. REFERENCE WEBSITES:

     1, baiyujia.com:
     http://www.baiyujia.com
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix86.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix105.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix244.asp

     2, foxite.com:
     https://www.foxite.com/archives/weird-bug-in-vfp-with-currency-datatype-0000493176.htm


6. OTHER:

     For reference only, there is no guarantees.

     Any questions or suggestions, please send me an email at ccb2000@163.com.