examples/Base/Locale/Currency/Currency.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "CommonFramework.h" // standard example framework
00017 
00018 // advance declarations
00019 void printCurrency();
00020 void formatCurrency(TDes &aBuffer, TReal currencyAmount);
00021 
00022 
00023 LOCAL_C void doExampleL()
00024     {
00025                 // construct and initialize application data
00026                 // Locale information includes whether there is a space between 
00027                 // currency symbol and amount, whether negative currency amounts 
00028                 // are enclosed in brackets, and whether digits to left of decimal 
00029                 // separator are grouped in threes ("Triads"). 
00030         TLocale locale; // locale information
00031         TCurrencySymbol currencySymbol;
00032         currencySymbol.Set();   // Get system wide currency symbol setting
00033         locale.SetCurrencySymbolPosition(ELocaleBefore); 
00034         locale.SetCurrencySpaceBetween(EFalse); 
00035         locale.SetNegativeCurrencyFormat(TLocale::ELeadingMinusSign);
00036         locale.SetCurrencyDecimalPlaces(2);     
00037         locale.SetCurrencyTriadsAllowed(ETrue); 
00038         locale.SetThousandsSeparator(',');
00039         locale.SetDecimalSeparator('.');
00040         locale.Set();                   // set system default settings
00041         printCurrency();
00042         }
00043 
00044 void printCurrency()
00045         {
00046         TBuf<30> aBuffer; // receives formatted currency string
00047         aBuffer.Zero(); // empty buffer
00048         TReal currencyAmount=-12345678.119;
00049         formatCurrency(aBuffer, currencyAmount);
00050         _LIT(KFormat1,"Currency value is: %S\n");
00051         console->Printf(KFormat1,&aBuffer);
00052         }
00053                 
00054 void formatCurrency(TDes &aBuffer, TReal currencyAmount)
00055         {
00056                 //
00057                 // Format the currency starting with the currency symbol 
00058                 //
00059         TLocale locale;                 // System locale settings
00060         TRealFormat realFormat; 
00061                 //
00062                 // Set up a TRealFormat object from locale information.
00063                 // This involves setting decimal and thousands separators, 
00064                 // whether triads are allowed or not and number of decimal places.
00065                 //
00066         realFormat.iType=KRealFormatFixed; // converts number to the form
00067                                            //"nnn.ddd" (n=integer, d=decimal) 
00068         realFormat.iWidth=30;              // Max. number of characters allowed
00069                                                                            // to  represent the number
00070         realFormat.iPlaces=locale.CurrencyDecimalPlaces(); 
00071         realFormat.iPoint=locale.DecimalSeparator(); 
00072         realFormat.iTriad=locale.ThousandsSeparator();
00073         realFormat.iTriLen=(locale.CurrencyTriadsAllowed() ? 1 : 0); 
00074         TCurrencySymbol symbol;                   // get currency symbol from
00075                                                                           // system setting
00076                         
00077         _LIT(KTxtOpenBra,"(");
00078         _LIT(KTxtSpace," ");
00079         _LIT(KTxtCloseBra,")");
00080         _LIT(KTxtMinusSign,"-");
00081                                                                           // Negative currency amounts may
00082                                                                           // be enclosed in brackets.
00083                                                                           // Currency symbol can appear before or
00084                                           // after the value.
00085                                           // We can have spaces between the currency
00086                                           // symbol and the value.
00087                                          
00088                                       
00089     TUint currencySymbolAtFront;
00090     TUint spaceBetweenSymbolAndValue;
00091 
00092                                       //
00093                                       // setup some useful values.    
00094                                       //
00095     currencySymbolAtFront      = ((locale.CurrencySymbolPosition()==ELocaleBefore) ? 0x01 : 0x00);
00096     spaceBetweenSymbolAndValue = (locale.CurrencySpaceBetween() ? 0x01 : 0x00);
00097                                       
00098                                       //
00099                                       // Deal with negative values
00100                                       //
00101     if (currencyAmount<0)
00102         {
00103                                       // Check if position of currency symbol needs to swap for 
00104                                       // for negavtive values
00105         currencySymbolAtFront ^= (locale.NegativeCurrencySymbolOpposite() ? 0x01 : 0x00);
00106         
00107                                       // Check if we need a space between currency symbol
00108                                       // and value.
00109         if (spaceBetweenSymbolAndValue && locale.NegativeLoseSpace())
00110             {
00111             spaceBetweenSymbolAndValue = 0x00;
00112             }
00113         
00114                                       // Now lay out the negative value as instructed. 
00115         switch (locale.NegativeCurrencyFormat())
00116             {
00117             case TLocale::ELeadingMinusSign :
00118                 {
00119                 aBuffer.Append(KTxtMinusSign);
00120                     if (currencySymbolAtFront)
00121                         {
00122                         aBuffer.Append(symbol);
00123                         if (spaceBetweenSymbolAndValue)
00124                             {
00125                             aBuffer.Append(KTxtSpace);
00126                             }
00127                         aBuffer.AppendNum(-currencyAmount,realFormat);
00128                         }
00129                     else
00130                         {
00131                         aBuffer.AppendNum(-currencyAmount,realFormat);
00132                         if (spaceBetweenSymbolAndValue)
00133                             {
00134                             aBuffer.Append(KTxtSpace);
00135                             }
00136                         aBuffer.Append(symbol);
00137                         }
00138                 break;
00139                 }
00140                
00141             case TLocale::ETrailingMinusSign :
00142                 {
00143                 if (currencySymbolAtFront)
00144                     {
00145                     aBuffer.Append(symbol);
00146                     if (spaceBetweenSymbolAndValue)
00147                             {
00148                             aBuffer.Append(KTxtSpace);
00149                             }
00150                         aBuffer.AppendNum(-currencyAmount,realFormat);
00151                         aBuffer.Append(KTxtMinusSign);
00152                     }
00153                 else
00154                     {
00155                     aBuffer.AppendNum(-currencyAmount,realFormat);
00156                         aBuffer.Append(KTxtMinusSign);
00157                         if (spaceBetweenSymbolAndValue)
00158                             {
00159                             aBuffer.Append(KTxtSpace);
00160                             }
00161                         aBuffer.Append(symbol);
00162                     }
00163                 break;
00164                 }
00165             
00166             case TLocale::EInterveningMinusSign :
00167                 {
00168                 if (currencySymbolAtFront)
00169                     {
00170                     aBuffer.Append(symbol);
00171                     if (spaceBetweenSymbolAndValue)
00172                             {
00173                             aBuffer.Append(KTxtSpace);
00174                             }
00175                         aBuffer.AppendNum(currencyAmount,realFormat);        
00176                     }
00177                 else
00178                     {
00179                     aBuffer.AppendNum(currencyAmount,realFormat);        
00180                     if (spaceBetweenSymbolAndValue)
00181                             {
00182                             aBuffer.Append(KTxtSpace);
00183                             }
00184                         aBuffer.Append(symbol);
00185                     }
00186                 break;
00187                 }
00188                 
00189             default : // EInBrackets is the only remaining option
00190                 {
00191                 aBuffer.Append(KTxtOpenBra);
00192                 if (currencySymbolAtFront)
00193                     {
00194                     aBuffer.Append(symbol);
00195                     if (spaceBetweenSymbolAndValue)
00196                             {
00197                             aBuffer.Append(KTxtSpace);
00198                             }
00199                     aBuffer.AppendNum(-currencyAmount,realFormat);
00200                     }
00201                 else
00202                     {
00203                     aBuffer.AppendNum(-currencyAmount,realFormat);
00204                     if (spaceBetweenSymbolAndValue)
00205                             {
00206                             aBuffer.Append(KTxtSpace);
00207                             }
00208                     aBuffer.Append(symbol);
00209                     }
00210                         aBuffer.Append(KTxtCloseBra);
00211                         break;
00212                 }
00213             
00214             }
00215         }
00216         
00217                                       //
00218                                       // Deal with zero or postive values
00219                                       //
00220     else
00221         {
00222         if (currencySymbolAtFront)
00223             {
00224             aBuffer.Append(symbol);
00225             if (spaceBetweenSymbolAndValue)
00226                     {
00227                     aBuffer.Append(KTxtSpace);
00228                     }
00229             aBuffer.AppendNum(currencyAmount,realFormat);
00230             }
00231         else
00232             {
00233             aBuffer.AppendNum(currencyAmount,realFormat);
00234             if (spaceBetweenSymbolAndValue)
00235                     {
00236                     aBuffer.Append(KTxtSpace);
00237                     }
00238             aBuffer.Append(symbol);
00239             }
00240         }
00241                 
00242         }       

Generated by  doxygen 1.6.2