< C++ .NET Formatting 2 | Index #1 | Index # 2 | C++ .NET Formatting 4 >


 

 

Playing With C++ .NET Formatting 3

 

 

The following are the topics available in this part.

  1. Standard Numeric Format Strings (continue)

  2. Standard Numeric Format Strings Output Examples

 

 

The following code example shows that CultureInfo.Clone also clones the DateTimeFormatInfo and NumberFormatInfo instances associated with the CultureInfo.

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Globalization;

int main()

{

   // Creates and initializes a CultureInfo.

   CultureInfo^ myCI = gcnew CultureInfo( "en-US", false );

   // Clones myCI and modifies the DTFI and NFI instances associated with the clone.

   CultureInfo^ myCIclone = dynamic_cast<CultureInfo^>(myCI->Clone());

   myCIclone->DateTimeFormat->AMDesignator = "a.m.";

   myCIclone->DateTimeFormat->DateSeparator = "-";

   myCIclone->NumberFormat->CurrencySymbol = "USD";

   myCIclone->NumberFormat->NumberDecimalDigits = 4;

   // Displays the properties of the DTFI and NFI

   // instances associated with the original and with the clone.

   Console::WriteLine("DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" );

   Console::WriteLine("=================\t========\t==============" );

   Console::WriteLine("DTFI.AMDesignator\t{0}\t\t{1}", myCI->DateTimeFormat->AMDesignator, myCIclone->DateTimeFormat->AMDesignator );

   Console::WriteLine("DTFI.DateSeparator\t{0}\t\t{1}", myCI->DateTimeFormat->DateSeparator, myCIclone->DateTimeFormat->DateSeparator );

   Console::WriteLine("NFI.CurrencySymbol\t{0}\t\t{1}", myCI->NumberFormat->CurrencySymbol, myCIclone->NumberFormat->CurrencySymbol );

   Console::WriteLine("NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI->NumberFormat->NumberDecimalDigits, myCIclone->NumberFormat->NumberDecimalDigits );

   return 0;

}

 

Output:

C++ .NET formatting - code example that shows the CultureInfo.Clone console program output sample

 

The following code example demonstrates the effect of changing the CurrencyDecimalDigits property.

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Globalization;

 

int main()

{

   // Gets a NumberFormatInfo associated with the en-US culture.

   CultureInfo^ MyCI = gcnew CultureInfo("en-US",false);

   NumberFormatInfo^ nfi = MyCI->NumberFormat;

   // Displays a negative value with the default number of decimal digits (2).

   Int64 myInt = -1234;

   Console::WriteLine(myInt.ToString("C", nfi));

   // Displays the same value with four decimal digits.

   nfi->CurrencyDecimalDigits = 4;

   Console::WriteLine(myInt.ToString("C", nfi));

   return 0;

}

 

Output:

C++ .NET formating - example demonstrates the effect of changing the CurrencyDecimalDigits property

 

The following code example demonstrates the effect of changing the NumberDecimalSeparator property.

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Globalization;

 

int main()

{

  // Gets a NumberFormatInfo associated with the en-US culture.

  CultureInfo^ MyCI = gcnew CultureInfo("en-US",false);

  NumberFormatInfo^ nfi = MyCI->NumberFormat;

  // Displays a value with the default separator (S".").

  Int64 myInt = 123456789;

  Console::WriteLine(myInt.ToString("N", nfi));

  // Displays the same value with a blank as the separator.

  nfi->NumberDecimalSeparator = " ";

  Console::WriteLine(myInt.ToString("N", nfi));

  return 0;

}

 

Output:

C++ .NET formatting - code example demonstrates the effect of changing the NumberDecimalSeparator property

 

The following code example shows how to create a CultureInfo for "Spanish - Spain" with the international sort and another CultureInfo with the traditional sort.

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Collections;

using namespace System::Globalization;

 

int main()

{

      // Creates and initializes the CultureInfo which uses the international sort.

      CultureInfo^ myCIintl = gcnew CultureInfo("es-ES", false);

      // Creates and initializes the CultureInfo which uses the traditional sort.

      CultureInfo^ myCItrad = gcnew CultureInfo(0x040A, false);

      // Displays the properties of each culture.

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "PROPERTY", "INTERNATIONAL", "TRADITIONAL");

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "========", "=============", "===========");

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "CompareInfo", myCIintl->CompareInfo, myCItrad->CompareInfo);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "DisplayName", myCIintl->DisplayName, myCItrad->DisplayName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "EnglishName", myCIintl->EnglishName, myCItrad->EnglishName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "IsNeutralCulture", myCIintl->IsNeutralCulture, myCItrad->IsNeutralCulture);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "IsReadOnly", myCIintl->IsReadOnly, myCItrad->IsReadOnly);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "LCID", myCIintl->LCID, myCItrad->LCID);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "Name", myCIintl->Name, myCItrad->Name);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "NativeName", myCIintl->NativeName, myCItrad->NativeName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "Parent", myCIintl->Parent, myCItrad->Parent);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "TextInfo", myCIintl->TextInfo, myCItrad->TextInfo);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterISOLanguageName", myCIintl->ThreeLetterISOLanguageName, myCItrad->ThreeLetterISOLanguageName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterWindowsLanguageName", myCIintl->ThreeLetterWindowsLanguageName, myCItrad->ThreeLetterWindowsLanguageName);

      Console::WriteLine("{0,-33}{1,-25}{2,-25}", "TwoLetterISOLanguageName", myCIintl->TwoLetterISOLanguageName, myCItrad->TwoLetterISOLanguageName);

      Console::WriteLine();

      // Compare two strings using myCIintl ->

      Console::WriteLine("Comparing \"llegar\" and \"lugar\"" );

      Console::WriteLine("   With myCIintl->CompareInfo->Compare: {0}", myCIintl->CompareInfo->Compare("llegar", "lugar"));

      Console::WriteLine("   With myCItrad->CompareInfo->Compare: {0}", myCItrad->CompareInfo->Compare("llegar", "lugar"));

      return 0;

}

 

Output:

 

 

 

 

 

C++ .net formatting - example shows how to create a CultureInfo for "Spanish - Spain" with the international sort and another CultureInfo with the traditional sort

 

The following code example determines the parent culture of each specific culture using the Chinese language.

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Globalization;

 

int main()

{

   // Prints the header.

   Console::WriteLine("SPECIFIC CULTURE                                  PARENT CULTURE");

   Console::WriteLine("================                                  ==============");

   // Determines the specific cultures that use the Chinese language,

   // and displays the parent culture.

   System::Collections::IEnumerator^ en = CultureInfo::GetCultures(CultureTypes::SpecificCultures)->GetEnumerator();

 

   while (en->MoveNext())

   {

      CultureInfo^ ci = safe_cast<CultureInfo^>(en->Current);

      if (ci->TwoLetterISOLanguageName->Equals("zh"))

      {

        Console::Write("0x{0} {1} {2,-37}", ci->LCID.ToString("X4"), ci->Name, ci->EnglishName);

        Console::WriteLine("0x{0} {1} {2}", ci->Parent->LCID.ToString("X4"), ci->Parent->Name, ci->Parent->EnglishName);

       }

   }

   return 0;

}

 

Output:

 

C++ .Net formatting - code example determines the parent culture of each specific culture using the Chinese language

 

Standard Numeric Format Strings Output Examples

 

The following table illustrates the output created by applying some standard numeric format strings to specific data types and values. Output was produced using the ToString method. The Format column indicates the format string, the Culture column indicates the culture under which the value was formatted, the Data type column indicates the data type used, the Value column indicates the value of the number being formatted, and the Output column indicates the result of formatting.

 

 

 

 

Format

Culture

Data type

Value

Output

C

en-US

Double

12345.6789

$12,345.68

C

de-DE

Double

12345.678

12.345,68 €

D

en-US

Int32

12345

12345

D8

en-US

Int32

12345

00012345

E

en-US

Double

12345.6789

1.234568E+004

E10

en-US

Double

12345.6789

1.2345678900E+004

E

fr-FR

Double

12345.6789

1,234568E+004

e4

en-US

Double

12345.6789

1.2346e+004

F

en-US

Double

12345.6789

12345.68

F

es-ES

Double

12345.6789

12345,68

F0

en-US

Double

12345.6789

123456

F6

en-US

Double

12345.6789

12345.678900

G

en-US

Double

12345.6789

12345.6789

G7

en-US

Double

12345.6789

12345.68

G

en-US

Double

0.0000023

2.3E-6

G

en-US

Double

0.0023

0.0023

G2

en-US

Double

1234

1.2E3

G

en-US

Double

Math.PI

3.14159265358979

N

en-US

Double

12345.6789

12,345.68

N

sv-SE

Double

12345.6789

12 345,68

N4

en-US

Double

123456789

123,456,789.0000

P

en-US

Double

.126

12.60 %

r

en-US

Double

Math.PI

3.141592653589793

x

en-US

Int32

0x2c45e

2c45e

X

en-US

Int32

0x2c45e

2C45E

X8

en-US

Int32

0x2c45e

0002C45E

x

en-US

Int32

123456789

75bcd15

 

Table 2

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9


< C++ .NET Formatting 2 | Index #1 | Index # 2 | C++ .NET Formatting 4 >