home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / ScriptBuilder / NOSB30_TRIAL.exe / data1.cab / Program_Files / CompLib / currency.js < prev    next >
Encoding:
Text File  |  1998-10-05  |  5.6 KB  |  142 lines

  1. function com_netobjects_currencyComponent( params ) {
  2.  
  3.     this.centsToDollars = centsToDollars;
  4.     this.formatMoneyStr = formatMoneyStr;
  5.    this.setCents = setCents;
  6.     this.setMoneyToFormat = setMoneyToFormat;
  7.  
  8.     this.centValue = (params.centValue+"" != "undefined" ? params.centValue : "");
  9.     this.moneyValue = (params.moneyValue+"" != "undefined" ? params.moneyValue : "");
  10.     this.name = (params.name+"" != "undefined" ? params.name : "currencyComponent1");
  11.      this.decimalSeparator = (params.decimalSeparator+"" != "undefined" ? params.decimalSeparator : ".");
  12.      this.thousandSeparator = (params.thousandSeparator+"" != "undefined" ? params.thousandSeparator : ",");
  13.     
  14.     /* ======================================================================
  15.     FUNCTION:    setCemts
  16.     
  17.     INPUT:        cents (int/string) - an integer or string representing a value in cents
  18.                     with no decimal point in the string
  19.     
  20.     RETURNS:        None
  21.     
  22.     DESC:            Sets the cents within the component that is used to convert into a string 
  23.                     representing dollars and cents.  For example, converts 125 cents to
  24.                    1.25 dollars.  
  25.     ====================================================================== */
  26.  
  27.    function setCents(value) {
  28.       if(value+"" != "null" && value+"" != "undefined")
  29.        this.centsValue = value;
  30.    }
  31.  
  32.     /* ======================================================================
  33.     FUNCTION:    setMoneyToFormat
  34.     
  35.     INPUT:        str (string) - a string representing a money value in dollars
  36.                                        or dollars and cents
  37.     
  38.     RETURNS:        None
  39.     
  40.     DESC:            Sets the moneyValue within the component that is used to convert into a string 
  41.                     representing dollars and cents.  e.g. $12.00.
  42.                     This prevents oddly formatted values such as $12.5 by replacing it with
  43.                     $12.50.  Note that this function does not add the '$' character.
  44.     ====================================================================== */
  45.  
  46.    function setMoneyToFormat(value) {
  47.       if(value+"" != null && value+"" != "undefined")
  48.        this.moneyValue = value;
  49.    }
  50.  
  51.     /* ======================================================================
  52.     FUNCTION:    centsToDollars
  53.     
  54.     INPUT:        cents (int/string) - an integer or string representing a value in cents
  55.                     with no decimal point in the string
  56.     
  57.     RETURNS:        a string representing the input value in dollars;
  58.                    if 'cents' is null or undefined, returns -1.
  59.     
  60.     DESC:            Converts an integer cents value into a string representing
  61.                    dollars and cents.  For example, converts 125 cents to
  62.                    1.25 dollars.  
  63.     ====================================================================== */
  64.     function centsToDollars() {
  65.         var resultStr = "0" + this.decimalSeparator + "00";
  66.         
  67.         // Return immediately if an invalid value was passed in
  68.         if (this.centValue+"" == "undefined" || this.centValue+"" == "null")    
  69.             return null;
  70.         
  71.         // Convert to string in case we were passed a numeric value
  72.         this.centValue = this.centValue + "";     
  73.         
  74.         if (this.centValue.length == 1) // handle case where value is 1 to 9 cents
  75.             resultStr = "0" + this.decimalSeparator + "0" + this.centValue;
  76.         else {
  77.           if (this.centValue.length == 2) // handle case where value is 10 to 99 cents
  78.             resultStr = "0" + this.decimalSeparator + this.centValue;
  79.           else {
  80.             if ( this.centValue.length > 5) {
  81.               resultStr = this.centValue.substring(0, this.centValue.length - 5) + this.thousandSeparator +
  82.               this.centValue.substring(this.centValue.length - 5, this.centValue.length - 2) + this.decimalSeparator +
  83.                   this.centValue.substring(this.centValue.length - 2, this.centValue.length);
  84.                }
  85.                else {
  86.               resultStr = this.centValue.substring(0, this.centValue.length - 2) + this.decimalSeparator +
  87.                   this.centValue.substring(this.centValue.length - 2, this.centValue.length);
  88.              }
  89.           }
  90.         }
  91.                  
  92.        return resultStr;
  93.     } // end centsToDollars
  94.     
  95.     /* ======================================================================
  96.     FUNCTION:    formatMoneyStr
  97.     
  98.     INPUT:        str (string) - a string representing a money value in dollars
  99.                                        or dollars and cents
  100.     
  101.     RETURNS:        a string representing the input value in dollars
  102.                     with two decimal places for cents;
  103.                     returns null if invalid arguments were passed
  104.     
  105.     DESC:            This function ensures all values of dollars and cents appear with two
  106.                     decimal places representing cents after the dollar amount, e.g. $12.00.
  107.                     This prevents oddly formatted values such as $12.5 by replacing it with
  108.                     $12.50.  Note that this function does not add the '$' character.
  109.     ====================================================================== */
  110.     function formatMoneyStr() {
  111.         resultStr = "";
  112.         resultStr2 = "";    
  113.  
  114.         // Return immediately if an invalid value was passed in
  115.         if (this.moneyValue+"" == "undefined" || this.moneyValue+"" == "null" || this.moneyValue+"" == "")    
  116.             return null;
  117.         
  118.         // Make sure argument is a string
  119.         this.moneyValue += "";
  120.         
  121.         // Get the index of the decimal point.
  122.         idx = this.moneyValue.indexOf(this.decimalSeparator);
  123.           idx2 = this.moneyValue.indexOf(this.thousandSeparator);
  124.         
  125.         // If there is no decimal point, add ".00"
  126.         if (idx < 0)
  127.             resultStr = this.moneyValue + this.decimalSeparator + "00";
  128.         else
  129.         {
  130.             resultStr = this.moneyValue.substring(0, idx + 3);
  131.             if (resultStr.length < (idx + 3)) 
  132.                 resultStr += "0";
  133.         }
  134.  
  135.         if( idx2 < 0 && resultStr.length > 6)
  136.             resultStr2 = resultStr.substring(0, resultStr.length - 6) + this.thousandSeparator +
  137.                         resultStr.substring(resultStr.length - 6, resultStr.length)
  138.         else
  139.              resultStr2 = resultStr;
  140.         return resultStr2;
  141.     } // end formatMoneyStr
  142. }