Ignore:
Timestamp:
Mar 30, 2018, 8:38:35 PM (8 years ago)
Author:
Alex Taylor
Message:

Make Sys2FormatNumber support negative values

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rxutilex/trunk/rxutilex.c

    r41 r42  
    10671067 * must not contain any separators already, and any decimal point which it   *
    10681068 * contains must be a period (rather than any localized decimal symbol).     *
     1069 * The maximum value is that of a signed 64-bit integer, or a long double.   *
    10691070 *                                                                           *
    10701071 * REXX ARGUMENTS:                                                           *
     
    10811082    PSZ   pszSep = NULL;                // Thousands separator string
    10821083    PSZ   pszDec = NULL;                // Decimal point string
     1084    PSZ   pszNeg = NULL;                // Negative sign string
    10831085#ifdef LEGACY_C_LOCALE
    10841086    float fVal = 0;                     // Input value as floating point
    10851087    int   iVal;                         // Input value as integer
    10861088#else
    1087     ULONGLONG   llVal  = 0;
     1089    LONGLONG    llVal  = 0;
    10881090    long double ldVal  = 0,
    10891091                ldFrac = 0;
    1090     PSZ   p;
    1091     CHAR  achFrac[ 16 ];
    1092     int   rc     = 0;
     1092    PSZ         p,
     1093                a;
     1094    BOOL        bNeg   = FALSE;
     1095    CHAR        achFrac[ 16 ];
     1096    int         rc     = 0;
    10931097#endif
    10941098
     
    11781182    }
    11791183
     1184    // Return the formatted number
     1185    SaveResultString( prsResult, achNumber, strlen(achNumber) );
     1186
    11801187#else
    11811188
     
    11841191        // GetLocaleString has already set the error indicator
    11851192        SaveResultString( prsResult, NULL, 0 );
    1186         return ( 0 );
     1193        goto finish;
    11871194    }
    11881195    rc = GetLocaleString( &pszDec, LOCI_sDecimal );
     
    11901197        // GetLocaleString has already set the error indicator
    11911198        SaveResultString( prsResult, NULL, 0 );
    1192         return ( 0 );
    1193     }
     1199        goto finish;
     1200    }
     1201    rc = GetLocaleString( &pszNeg, LOCI_sNegativeSign );
     1202    if ( rc ) {
     1203        // GetLocaleString has already set the error indicator
     1204        SaveResultString( prsResult, NULL, 0 );
     1205        goto finish;
     1206    }
     1207
     1208    // Check for negative value
     1209    //if ( argv[0].strptr == '-') bNeg = TRUE;
    11941210
    11951211    // Check for a decimal place and treat as float or integer accordingly
     
    12071223        else
    12081224            sprintf( achFrac, "%.2Lf", ldFrac );
     1225
    12091226        // Format the integer part
    1210         GroupNumber( achNumber, llVal, pszSep );
     1227        a = achNumber;
     1228        if ( llVal < 0 ) {
     1229            sprintf( a, pszNeg );
     1230            a += strlen( pszNeg );
     1231        }
     1232        GroupNumber( a, llabs( llVal ), pszSep );
    12111233        // Append the fractional part
    12121234        if (( p = strchr( achFrac, '.')) != NULL ) {
     
    12171239    else {
    12181240        if (( sscanf( argv[0].strptr, "%lld", &llVal )) != 1 ) return ( 40 );
    1219         GroupNumber( achNumber, llVal, pszSep );
    1220     }
    1221 
    1222     free( pszSep );
     1241        a = achNumber;
     1242        if ( llVal < 0 ) {
     1243            sprintf( a, pszNeg );
     1244            a += strlen( pszNeg );
     1245        }
     1246        GroupNumber( a, llabs( llVal ), pszSep );
     1247    }
     1248
     1249    // Return the formatted number
     1250    SaveResultString( prsResult, achNumber, strlen(achNumber) );
     1251
     1252finish:
     1253    if ( pszSep ) free( pszSep );
     1254    if ( pszDec ) free( pszDec );
     1255    if ( pszNeg ) free( pszNeg );
     1256
    12231257#endif
    1224 
    1225     // Return the formatted number
    1226     SaveResultString( prsResult, achNumber, strlen(achNumber) );        // 2016-02-20 SHL
    12271258
    12281259    return ( 0 );
Note: See TracChangeset for help on using the changeset viewer.