home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / rexx / api / rxmacdll / nlsmoney.cmd < prev    next >
OS/2 REXX Batch file  |  1999-05-11  |  8KB  |  144 lines

  1. /*******************************************************************/
  2. /*                                                                 */
  3. /* Function:            NLSMONEY                                   */
  4. /*                                                                 */
  5. /* Description:         Format currency amounts according to NLS   */
  6. /*                      conventions.                               */
  7. /*                                                                 */
  8. /* Input:               Amount                                     */
  9. /*                                                                 */
  10. /* Output:              Formatted amount                           */
  11. /*                                                                 */
  12. /* Effects:             None                                       */
  13. /*                                                                 */
  14. /* Notes:                                                          */
  15. /*                                                                 */
  16. /*    Requires the external function RxNLSInfo().  This function   */
  17. /*    resides in RXNLSINF.DLL                                      */
  18. /*                                                                 */
  19. /*    If the input string is not a valid amount, NLSMONEY returns  */
  20. /*    an empty string.  Valid amounts are nonnegative and not in   */
  21. /*    exponential notation.                                        */
  22. /*                                                                 */
  23. /*    NLSMONEY does the following:                                 */
  24. /*                                                                 */
  25. /*    1.  Initialize the return value to an empty string.          */
  26. /*                                                                 */
  27. /*    2.  Check the format of the amount.  It must be a number     */
  28. /*        greater than or equal to 0.                              */
  29. /*                                                                 */
  30. /*    3.  If the amount contains a leading '+' sign, drop it.      */
  31. /*        We do this by adding 0 to the amount.                    */
  32. /*                                                                 */
  33. /*    4.  Split the amount into its integral and fractional parts. */
  34. /*        validate the format of each part.  If the original       */
  35. /*        amount was in exponential form, it will fail this step.  */
  36. /*        If either part is empty, make it '0'.                    */
  37. /*                                                                 */
  38. /*    5.  If the external function RxNLSInfo() is not registered,  */
  39. /*        register it.                                             */
  40. /*                                                                 */
  41. /*    6.  Examine the fractional part.  If it is longer than two   */
  42. /*        digits, truncate to two digits.  If it is shorter than   */
  43. /*        two digits, pad with zeros.                              */
  44. /*                                                                 */
  45. /*    7.  Get the thousands separator.  Break the integral part    */
  46. /*        into groups of three digits, and insert the thousands    */
  47. /*        separator between each group of three digits.            */
  48. /*                                                                 */
  49. /*    8.  Assemble the formatted amount.                           */
  50. /*                                                                 */
  51. /*    9.  Return to the caller.                                    */
  52. /*                                                                 */
  53. /*******************************************************************/
  54.  
  55. arg amount
  56.  
  57. /********************************************************************/
  58. /* Initialize the function result to an empty string.               */
  59. /********************************************************************/
  60. nls_amount = ""
  61.  
  62. /********************************************************************/
  63. /* The amount must be a number greater than or equal to 0.  If not, */
  64. /* simply return the empty string set in the preceding step.        */
  65. /********************************************************************/
  66.  
  67. if datatype( amount, number ) then do
  68.    if 0 <= amount then do
  69.  
  70.       /**************************************************************/
  71.       /* Remove any leading '+' sign from the amount by adding 0 to */
  72.       /* the amount.                                                */
  73.       /**************************************************************/
  74.  
  75.       amount = amount + 0
  76.  
  77.       /**************************************************************/
  78.       /* Parse the amount into its integral and fractional parts.   */
  79.       /* If either part is empty, make it '0'.  Validate both       */
  80.       /* parts.                                                     */
  81.       /**************************************************************/
  82.  
  83.       parse value amount with integer '.' fraction
  84.  
  85.       if length( integer ) = 0 then integer = 0
  86.       if length( fraction ) = 0 then fraction = 0
  87.  
  88.       if datatype( integer, number ) & datatype( fraction, number ),
  89.          then do
  90.  
  91.          /***********************************************************/
  92.          /* If the external function RxNLSInfo() is not registered, */
  93.          /* do so now.  Signal failure if we cannot register the    */
  94.          /* external function.                                      */
  95.          /***********************************************************/
  96.  
  97.          if rxfuncquery( 'RxNLSInfo' ) then do
  98.             if rxfuncadd( 'RxNLSInfo', 'RxNLSInf', 'RxNLSInfo' ) then do
  99.                signal failure
  100.             end
  101.          end
  102.  
  103.          /***********************************************************/
  104.          /* If the fractional part is longer than two digits, trun- */
  105.          /* cate it.  If it is shorter than two digits, padd it     */
  106.          /* on the right with zeros.                                */
  107.          /***********************************************************/
  108.  
  109.          fraction = left( fraction, 2, '0' )
  110.  
  111.          /***********************************************************/
  112.          /* Break the integral portion of the amount into groups of */
  113.          /* three digits.  Separate the groups of three with the    */
  114.          /* thousand separator.  Note we parse right to left.  We   */
  115.          /* insert a thousand separator only if digits remain on    */
  116.          /* the left.                                               */
  117.          /***********************************************************/
  118.  
  119.          thousand = RxNLSInfo( thousand )
  120.          formint = ""
  121.  
  122.          do while 0 < length( integer )
  123.             lenofint = length( integer )
  124.             lenofgroup = min( lenofint, 3 )
  125.             formint = right( integer, lenofgroup ) || formint
  126.             lenofint = lenofint - lenofgroup
  127.             integer = left( integer, lenofint )
  128.             if 0 < lenofint then formint = thousand || formint
  129.          end
  130.  
  131.          /***********************************************************/
  132.          /* Assemble the formatted currency amount.                 */
  133.          /***********************************************************/
  134.  
  135.          nls_amount = RxNLSInfo( prefix ) || ,
  136.                       formint || ,
  137.                       RxNLSInfo( infix ) || ,
  138.                       fraction || ,
  139.                       RxNLSInfo( suffix )
  140.       end                              /* If integer & fraction OK. */
  141.    end                                 /* If amount >= 0            */
  142. end                                    /* If amount is numeric.     */
  143. return nls_amount
  144.