固有カルチャの数値データの書式指定

更新 : 2007 年 11 月

NumberFormatInfo クラスにより、カルチャに基づいた通貨、桁区切り記号などの数値記号の書式指定方法と表示方法が定義されます。たとえば 10000.50 という 10 進数の書式は、英語 (U.S.) カルチャ "en-US" の場合は 10,000.50 となり、ドイツ語 (ドイツ) カルチャ "de-DE" の場合は 10.000,50 となります。

NumberFormatInfo オブジェクトは、固有カルチャまたはインバリアント カルチャに対して作成でき、ニュートラル カルチャに対しては作成できません。ニュートラル カルチャには、正しい数値書式を表示するために必要な情報が不足しています。アプリケーションがニュートラル カルチャを使用して NumberFormatInfo オブジェクトを作成しようとすると、例外がスローされます。

次のコード例は、現在のカルチャに対して NumberFormatInfo 標準通貨書式 ("c") を使用して整数値を表示します。

Imports System
Imports System.Globalization

Public Class TestClass

   Public Shared Sub Main()
      Dim i As Integer = 100
      
      ' Creates a CultureInfo for English in Belize.
      Dim bz As New CultureInfo("en-BZ")
      ' Displays i formatted as currency for the bz.
      Console.WriteLine(i.ToString("c", bz))
      
      ' Creates a CultureInfo for English in the U.S.
      Dim us As New CultureInfo("en-US")
      ' Displays i formatted as currency for us.
      Console.WriteLine(i.ToString("c", us))
      
      ' Creates a CultureInfo for Danish in Denmark.
      Dim dk As New CultureInfo("da-DK")
      ' Displays i formatted as currency for dk.
      Console.WriteLine(i.ToString("c", dk))
   End Sub
End Class
using System;
using System.Globalization;

public class TestClass
{
   public static void Main()
   {
      int i = 100;
      
      // Creates a CultureInfo for English in Belize.
      CultureInfo bz = new CultureInfo("en-BZ");
      // Displays i formatted as currency for the bz.
      Console.WriteLine(i.ToString("c", bz));
      
      // Creates a CultureInfo for English in the U.S.
      CultureInfo us = new CultureInfo("en-US");
      // Display i formatted as currency for us.
      Console.WriteLine(i.ToString("c", us));
      
      // Creates a CultureInfo for Danish in Denmark.
      CultureInfo dk = new CultureInfo("da-DK");
      // Displays i formatted as currency for dk.
      Console.WriteLine(i.ToString("c", dk));
   }
}

このコードを実行すると、次の出力が生成されます。

BZ$100.00
$100.00
kr100,00

ユーロ圏の国の通貨書式の指定

CultureInfo クラスと RegionInfo クラスには、通貨に関する情報が格納されています。カルチャごとに指定できる通貨は 1 種類だけです。ユーロが公式通貨として制定されている国は、ベルギー、ドイツ、スペイン、フランス、アイルランド、イタリア、ルクセンブルグ、オランダ、オーストリア、ポルトガル、フィンランド、およびギリシャです。2002 年 1 月 1 日に、これらの国でユーロ紙幣とユーロ硬貨の使用が開始されました。このため、.NET Framework と Microsoft Windows XP では、この 12 か国については既定通貨記号がユーロに設定されています。旧バージョンの Windows では、これらの国の既定通貨記号が各国の現地通貨に設定されます。

オペレーティング システム間での既定通貨記号の違いについて認識しておいてください。Windows では、コントロール パネルの [地域と言語のオプション] を使用して、システムの既定のカルチャに関連付けられている値の一部をユーザーがオーバーライドできます。たとえば、表示する通貨記号として、カルチャの既定の記号以外の記号を使用できます。Windows フォーム アプリケーションおよびコンソール アプリケーションでは、オペレーティング システムで指定されている既定通貨記号が使用されます。ユーロ受け入れ国および地域のユーザーが旧バージョンの Windows を使用しており、コントロール パネルの [地域と言語のオプション] で通貨設定をユーロに更新していない場合には、既定通貨設定が不適切です。ASP.NET で作成された ASP.NET アプリケーションと XML Web サービス アプリケーションは、特定のユーザー向けサービスではなく、システム サービスとして稼動します。したがって、これらのアプリケーションが返す既定の結果は、Windows フォームおよびコンソール アプリケーションで返される結果とは異なります。

オペレーティング システム間の通貨設定の違いからアプリケーションを保護し、一貫した通貨書式を実現するために、カルチャに対して .NET Framework の既定通貨設定を使用するコードを作成することをお勧めします。アプリケーションは、useUserOverride パラメータを受け取るいずれかのコンストラクタ オーバーロードを使用して CultureInfo オブジェクトを作成し、このパラメータを false に設定する必要があります。この設定により、ユーザーのオペレーティング システムの既定通貨設定が .NET Framework の正しい既定の設定によってオーバーライドされます。

次の XML Web サービス コードでは、XML Web サービス メソッド UserLocalSetting が CurrentCulture プロパティをフランス語 (フランス) を示す "fr-FR" に設定し、通貨値として書式指定された整数値を取得します。オペレーティング システム間で通貨設定に違いがあるため、ユーロ通貨記号と記号 "F" のどちらが使用されるかは不確定です。XML Web サーバー メソッド OverrideUserSetting は、false に設定された useUserOverride パラメータを受け取る CultureInfo コンストラクタを使用して CurrentCulture プロパティを "fr-FR" に設定します。このメソッドは、通貨値として書式指定された整数値を取得します。この場合 .NET Framework の既定通貨記号であるユーロ記号が必ず使用されます。

 [Visual Basic]
<%@ WebService Language="VB" Class="userOverrideSample" %>

Imports System
Imports System.Web.Services
Imports System.Globalization
Imports System.Threading

Public Class userOverrideSample

   <WebMethod> _
   Public Function UserLocalSetting() As String
      Dim i As Integer = 100

      ' Sets the CurrentCulture to French in France.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
      ' Displays i formatted as currency for the CurrentCulture.
      ' Due to operating system differences, you cannot be sure what currency
      ' symbol will be used.
      return (i.ToString("c"))
   End Function

   <WebMethod> _
   Public Function OverrideUserSetting() As String
      Dim i As Integer = 100

      ' Sets the CurrentCulture to French in France.
      ' Uses the CultureInfo constructor that takes a 
      ' useUserOverride parameter.
      ' Sets the useUserOverride value to false.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR", _
         false)
      ' Displays i formatted as currency for the CurrentCulture.
      ' This will override any user settings and display the euro symbol.
      return (i.ToString("c"))
   End Function 
End Class
<%@ WebService Language="c#" Class="userOverrideSample" %>

using System;
using System.Web.Services;
using System.Globalization;
using System.Threading;

public class userOverrideSample
{
   [WebMethod]
   public String UserLocalSetting()
   {
      int i = 100;

      // Sets the CurrentCulture to French in France.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      // Displays i formatted as currency for the CurrentCulture.
      // Due to operating system differences, you cannot be sure what currency
      // symbol will be used.
      return (i.ToString("c"));
   }   
   
   [WebMethod]
   public String OverrideUserSetting()
   {
      int i = 100;

      // Sets the CurrentCulture to French in France.
      // Uses the CultureInfo constructor that takes a 
      // useUserOverride parameter.
      // Sets the useUserOverride value to false.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR", _
         false);
      // Displays i formatted as currency for the CurrentCulture.
      // This will override any user settings and display the euro symbol.
      return (i.ToString("c"));
   }
}

Windows オペレーティング システムのすべてのバージョンで稼動する Windows フォーム アプリケーションおよびコンソール アプリケーションでは、ユーザーのコンピュータの設定を使用して既定通貨記号が設定されます。前述したように、この設定は不適切である可能性があります。.NET Framework の既定の設定が必ず使用されるようにするには、アプリケーションが CultureInfo オブジェクトを作成し、useUserOverride パラメータを false に設定して渡す必要があります。

上記の例に類似したコードを使用した例を次に示します。現在のカルチャが "fr-FR" に設定され、通貨値として書式指定された整数値がコンソールに表示されます。ユーザーの現地通貨設定を使用して通貨書式が指定されます。次に、false に設定された useUserOverride パラメータを受け取る CultureInfo コンストラクタによって、カルチャが "fr-FR" に設定されます。.NET Framework の既定通貨設定を使用して数値の書式が指定され、ユーロ通貨記号が表示されます。

 [Visual Basic]
Imports System
Imports System.Globalization
Imports System.Threading

Public Class EuroSymbolSample
   Public Shared Sub Main()
      Dim i As Integer = 100
      
      ' Sets the CurrentCulture to French in France.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
      ' Displays i formatted as currency for the CurrentCulture.
      ' On a version of Windows prior to Windows XP, where the user
      ' has not changed the default currency to euro through the
      ' Control Panel, this will default to "F".
      Console.WriteLine(i.ToString("c"))
      
      
      ' Sets the CurrentCulture to French in France, using the
      ' CultureInfo constructor that takes a useUserOverride parameter.
      ' Sets the useUserOverride value to false. 
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR", _ 
         False)
      ' Displays i formatted as default currency for the CurrentCulture.
      ' On a version of Windows prior to Windows XP, this will override an
      ' incorrect default setting of "F" and display the euro symbol ().
      Console.WriteLine(i.ToString("c"))
   End Sub
End Class
using System;
using System.Globalization;
using System.Threading;

public class EuroSymbolSample
{
   public static void Main()
   {
      int i = 100;

      // Sets the CurrentCulture to French in France.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      // Displays i formatted as default currency for the CurrentCulture.
      // On a version of Windows prior to Windows XP, where the user
      // has not changed the default currency to euro through the
      // Control Panel, this will default to "F".
      Console.WriteLine(i.ToString("c"));

      // Sets the CurrentCulture to French in France, using the
      // CultureInfo constructor that takes a useUserOverride parameter.
      // Sets the useUserOverride value to false. 
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR", 
            false);
      // Displays i formatted as default currency for the CurrentCulture.
      // On a version of Windows prior to Windows XP, this will override an
      // incorrect default setting of "F" and display the euro symbol ().
      Console.WriteLine(i.ToString("c"));      
   }
} 

コンソール環境では、ユーロ文字がサポートされていません。Windows フォーム アプリケーションでこのコードを実行すると、出力は次のようになります。

100,00 F
100,00 €

ヨーロッパの多くの国では、一般的に 2 種類の通貨、つまりユーロと各国通貨が使用されています。場合によっては、アプリケーションで両方の通貨を表示する必要があります。既定通貨がユーロのカルチャ "fr-FR" に対して CultureInfo オブジェクトを作成するコード例を次に示します。現地通貨の通貨記号を表示するには、NumberFormatInfo.Clone メソッドを使用して、CultureInfo に対して NumberFormatInfo のクローンを新規に作成し、既定通貨記号を現地通貨記号に置き換えます。

Imports System
Imports System.Globalization
Imports System.Threading

Public Class EuroLocalSample
   Public Shared Sub Main()
      ' Creates a CultureInfo for French in France.
      Dim FrCulture As New CultureInfo("fr-FR")
      ' Sets the CurrentCulture to fr-FR.
      Thread.CurrentThread.CurrentCulture = FrCulture
      
      ' Clones the NumberFormatInfo and creates
      ' a new object for the local currency of France.
      Dim LocalFormat As NumberFormatInfo =_
         CType(NumberFormatInfo.CurrentInfo.Clone(), NumberFormatInfo)
      ' Replaces the default currency symbol 
      ' with the local currency symbol.
      LocalFormat.CurrencySymbol = "F"
      
      Dim i As Integer = 100
      ' Displays i formatted as the local currency.
      Console.WriteLine(i.ToString("c", LocalFormat))
      
      ' Displays i formatted as the default currency.
      Console.WriteLine(i.ToString("c", NumberFormatInfo.CurrentInfo))
   End Sub
End Class 
using System;
using System.Globalization;
using System.Threading;

public class EuroLocalSample
{
   public static void Main()
   {             
      // Creates a CultureInfo for French in France.
      CultureInfo FrCulture = new CultureInfo("fr-FR");
      // Sets the CurrentCulture to fr-FR.
      Thread.CurrentThread.CurrentCulture = FrCulture;

      // Clones the NumberFormatInfo and creates
      // a new object for the local currency of France.
      NumberFormatInfo LocalFormat = 
         (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
      // Replaces the default currency symbol with the 
      // local currency symbol.
      LocalFormat.CurrencySymbol = "F";

      int i = 100;

      // Displays i formatted as the local currency.
      Console.WriteLine(i.ToString("c", LocalFormat));

      // Displays i formatted as the default currency.
      Console.WriteLine(i.ToString("c", NumberFormatInfo.CurrentInfo));
   }
}

関連する例については、共通タスクのクイック スタートの MultiCurrency サンプルを参照してください。

参照

概念

カルチャ別の書式設定

その他の技術情報

エンコーディングとローカリゼーション

型の書式設定