examples/Base/Locale/Currency/Currency.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include "CommonFramework.h" // standard example framework
00032 
00033 // advance declarations
00034 void printCurrency();
00035 void formatCurrency(TDes &aBuffer, TReal currencyAmount);
00036 
00037 
00038 LOCAL_C void doExampleL()
00039     {
00040                 // construct and initialize application data
00041                 // Locale information includes whether there is a space between 
00042                 // currency symbol and amount, whether negative currency amounts 
00043                 // are enclosed in brackets, and whether digits to left of decimal 
00044                 // separator are grouped in threes ("Triads"). 
00045         TLocale locale; // locale information
00046         TCurrencySymbol currencySymbol;
00047         currencySymbol.Set();   // Get system wide currency symbol setting
00048         locale.SetCurrencySymbolPosition(ELocaleBefore); 
00049         locale.SetCurrencySpaceBetween(EFalse); 
00050         locale.SetNegativeCurrencyFormat(TLocale::ELeadingMinusSign);
00051         locale.SetCurrencyDecimalPlaces(2);     
00052         locale.SetCurrencyTriadsAllowed(ETrue); 
00053         locale.SetThousandsSeparator(',');
00054         locale.SetDecimalSeparator('.');
00055         locale.Set();                   // set system default settings
00056         printCurrency();
00057         }
00058 
00059 void printCurrency()
00060         {
00061         TBuf<30> aBuffer; // receives formatted currency string
00062         aBuffer.Zero(); // empty buffer
00063         TReal currencyAmount=-12345678.119;
00064         formatCurrency(aBuffer, currencyAmount);
00065         _LIT(KFormat1,"Currency value is: %S\n");
00066         console->Printf(KFormat1,&aBuffer);
00067         }
00068                 
00069 void formatCurrency(TDes &aBuffer, TReal currencyAmount)
00070         {
00071                 //
00072                 // Format the currency starting with the currency symbol 
00073                 //
00074         TLocale locale;                 // System locale settings
00075         TRealFormat realFormat; 
00076                 //
00077                 // Set up a TRealFormat object from locale information.
00078                 // This involves setting decimal and thousands separators, 
00079                 // whether triads are allowed or not and number of decimal places.
00080                 //
00081         realFormat.iType=KRealFormatFixed; // converts number to the form
00082                                            //"nnn.ddd" (n=integer, d=decimal) 
00083         realFormat.iWidth=30;              // Max. number of characters allowed
00084                                                                            // to  represent the number
00085         realFormat.iPlaces=locale.CurrencyDecimalPlaces(); 
00086         realFormat.iPoint=locale.DecimalSeparator(); 
00087         realFormat.iTriad=locale.ThousandsSeparator();
00088         realFormat.iTriLen=(locale.CurrencyTriadsAllowed() ? 1 : 0); 
00089         TCurrencySymbol symbol;                   // get currency symbol from
00090                                                                           // system setting
00091                         
00092         _LIT(KTxtOpenBra,"(");
00093         _LIT(KTxtSpace," ");
00094         _LIT(KTxtCloseBra,")");
00095         _LIT(KTxtMinusSign,"-");
00096                                                                           // Negative currency amounts may
00097                                                                           // be enclosed in brackets.
00098                                                                           // Currency symbol can appear before or
00099                                           // after the value.
00100                                           // We can have spaces between the currency
00101                                           // symbol and the value.
00102                                          
00103                                       
00104     TUint currencySymbolAtFront;
00105     TUint spaceBetweenSymbolAndValue;
00106 
00107                                       //
00108                                       // setup some useful values.    
00109                                       //
00110     currencySymbolAtFront      = ((locale.CurrencySymbolPosition()==ELocaleBefore) ? 0x01 : 0x00);
00111     spaceBetweenSymbolAndValue = (locale.CurrencySpaceBetween() ? 0x01 : 0x00);
00112                                       
00113                                       //
00114                                       // Deal with negative values
00115                                       //
00116     if (currencyAmount<0)
00117         {
00118                                       // Check if position of currency symbol needs to swap for 
00119                                       // for negavtive values
00120         currencySymbolAtFront ^= (locale.NegativeCurrencySymbolOpposite() ? 0x01 : 0x00);
00121         
00122                                       // Check if we need a space between currency symbol
00123                                       // and value.
00124         if (spaceBetweenSymbolAndValue && locale.NegativeLoseSpace())
00125             {
00126             spaceBetweenSymbolAndValue = 0x00;
00127             }
00128         
00129                                       // Now lay out the negative value as instructed. 
00130         switch (locale.NegativeCurrencyFormat())
00131             {
00132             case TLocale::ELeadingMinusSign :
00133                 {
00134                 aBuffer.Append(KTxtMinusSign);
00135                     if (currencySymbolAtFront)
00136                         {
00137                         aBuffer.Append(symbol);
00138                         if (spaceBetweenSymbolAndValue)
00139                             {
00140                             aBuffer.Append(KTxtSpace);
00141                             }
00142                         aBuffer.AppendNum(-currencyAmount,realFormat);
00143                         }
00144                     else
00145                         {
00146                         aBuffer.AppendNum(-currencyAmount,realFormat);
00147                         if (spaceBetweenSymbolAndValue)
00148                             {
00149                             aBuffer.Append(KTxtSpace);
00150                             }
00151                         aBuffer.Append(symbol);
00152                         }
00153                 break;
00154                 }
00155                
00156             case TLocale::ETrailingMinusSign :
00157                 {
00158                 if (currencySymbolAtFront)
00159                     {
00160                     aBuffer.Append(symbol);
00161                     if (spaceBetweenSymbolAndValue)
00162                             {
00163                             aBuffer.Append(KTxtSpace);
00164                             }
00165                         aBuffer.AppendNum(-currencyAmount,realFormat);
00166                         aBuffer.Append(KTxtMinusSign);
00167                     }
00168                 else
00169                     {
00170                     aBuffer.AppendNum(-currencyAmount,realFormat);
00171                         aBuffer.Append(KTxtMinusSign);
00172                         if (spaceBetweenSymbolAndValue)
00173                             {
00174                             aBuffer.Append(KTxtSpace);
00175                             }
00176                         aBuffer.Append(symbol);
00177                     }
00178                 break;
00179                 }
00180             
00181             case TLocale::EInterveningMinusSign :
00182                 {
00183                 if (currencySymbolAtFront)
00184                     {
00185                     aBuffer.Append(symbol);
00186                     if (spaceBetweenSymbolAndValue)
00187                             {
00188                             aBuffer.Append(KTxtSpace);
00189                             }
00190                         aBuffer.AppendNum(currencyAmount,realFormat);        
00191                     }
00192                 else
00193                     {
00194                     aBuffer.AppendNum(currencyAmount,realFormat);        
00195                     if (spaceBetweenSymbolAndValue)
00196                             {
00197                             aBuffer.Append(KTxtSpace);
00198                             }
00199                         aBuffer.Append(symbol);
00200                     }
00201                 break;
00202                 }
00203                 
00204             default : // EInBrackets is the only remaining option
00205                 {
00206                 aBuffer.Append(KTxtOpenBra);
00207                 if (currencySymbolAtFront)
00208                     {
00209                     aBuffer.Append(symbol);
00210                     if (spaceBetweenSymbolAndValue)
00211                             {
00212                             aBuffer.Append(KTxtSpace);
00213                             }
00214                     aBuffer.AppendNum(-currencyAmount,realFormat);
00215                     }
00216                 else
00217                     {
00218                     aBuffer.AppendNum(-currencyAmount,realFormat);
00219                     if (spaceBetweenSymbolAndValue)
00220                             {
00221                             aBuffer.Append(KTxtSpace);
00222                             }
00223                     aBuffer.Append(symbol);
00224                     }
00225                         aBuffer.Append(KTxtCloseBra);
00226                         break;
00227                 }
00228             
00229             }
00230         }
00231         
00232                                       //
00233                                       // Deal with zero or postive values
00234                                       //
00235     else
00236         {
00237         if (currencySymbolAtFront)
00238             {
00239             aBuffer.Append(symbol);
00240             if (spaceBetweenSymbolAndValue)
00241                     {
00242                     aBuffer.Append(KTxtSpace);
00243                     }
00244             aBuffer.AppendNum(currencyAmount,realFormat);
00245             }
00246         else
00247             {
00248             aBuffer.AppendNum(currencyAmount,realFormat);
00249             if (spaceBetweenSymbolAndValue)
00250                     {
00251                     aBuffer.Append(KTxtSpace);
00252                     }
00253             aBuffer.Append(symbol);
00254             }
00255         }
00256                 
00257         }       

Generated by  doxygen 1.6.2