다음을 통해 공유


특정 문화권의 숫자 데이터 형식 지정

업데이트: 2007년 11월

NumberFormatInfo 클래스는 문화권에 따라 통화, 소수 구분 기호 및 기타 숫자 기호에 형식을 지정하여 표시하는 방법을 정의합니다. 예를 들어, 10진수 10000.50은 영어(미국) "en-US" 문화권인 경우에는 10,000.50으로, 독일어(독일) "de-DE" 문화권인 경우에는 10.000,50으로 형식이 지정됩니다.

중립 문화권이 아닌 고정 문화권이나 특정 문화권에 대해서만 NumberFormatInfo 개체를 만들 수 있습니다. 중립 문화권에서는 정확한 숫자 형식을 표시할 충분한 정보가 제공되지 않습니다. 응용 프로그램에서 중립 문화권을 사용하여 NumberFormatInfo 개체를 만들려고 하면 예외가 throw됩니다.

다음 코드 예제에서는 지정한 문화권의 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

유럽 국가의 통화 형식 지정

CultureInfoRegionInfo 클래스 모두에는 통화 관련 정보가 포함됩니다. 문화권당 하나의 통화만 지정됩니다. 유로는 벨기에, 독일, 스페인, 프랑스, 아일랜드, 이탈리아, 룩셈부르크, 네덜란드, 오스트리아, 포르투갈, 핀란드 및 그리스의 공식 통화입니다. 이러한 국가에서는 2002년 1월 1일부터 유로 지폐와 동전을 사용하기 시작했습니다. 따라서 .NET Framework 및 Microsoft Windows XP에서는 이러한 12개 국가의 기본 통화 기호가 유로로 설정됩니다. 이전 Windows 버전에서는 이러한 국가의 기본 통화 기호가 계속 현지 통화로 설정됩니다.

운영 체제 간에 기본 통화 기호가 다르다는 점을 알고 있어야 합니다. Windows 사용자는 제어판의 국가 및 언어 옵션을 통해 운영 체제의 기본 문화권과 관련된 일부 값을 재정의할 수 있습니다. 예를 들어, 통화를 표시할 때 문화권에 대해 기본값 이외의 기호를 사용하도록 선택할 수 있습니다. Windows Forms 및 콘솔 응용 프로그램에서는 운영 체제에 지정된 기본 통화 기호를 사용합니다. 유로화를 사용하는 국가 또는 지역에서 이전 버전의 Windows를 사용하는 경우 제어판의 국가 및 언어 옵션을 통해 통화 설정을 유로로 업데이트하지 않으면 기본 통화가 잘못 설정됩니다. ASP.NET 응용 프로그램 및 ASP.NET에서 만들어진 XML Web services 응용 프로그램은 특정 사용자를 위해서가 아니라 시스템 서비스로서 실행됩니다. 따라서 반환되는 기본 결과가 Windows Forms 및 콘솔 응용 프로그램에서 반환되는 결과와 다를 수 있습니다.

이러한 운영 체제 간의 차이가 응용 프로그램에 반영되지 않게 하고 통화 형식을 일관적으로 지정하려면 문화권에 대한 .NET Framework의 기본 통화 설정을 사용하는 코드를 작성하는 것이 좋습니다. 응용 프로그램에서 생성자 오버로드 중 useUserOverride 매개 변수를 받는 오버로드를 사용하여 CultureInfo 개체를 만들고 이 매개 변수를 false로 설정합니다. 이렇게 설정하면 사용자 운영 체제의 기본 통화 설정이 .NET Framework의 올바른 기본 설정으로 재정의됩니다.

다음 XML Web services 코드에서 UserLocalSetting XML Web services 메서드는 CurrentCulture 속성을 프랑스어(프랑스) "fr-FR"로 설정하고 통화 형식으로 지정된 정수를 검색합니다. 운영 체제 차이로 인해 사용될 기호가 유로 기호인지 아니면 "F" 기호인지 확인할 수는 없습니다. OverrideUserSetting XML 웹 서버 메서드는 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 Forms 및 콘솔 응용 프로그램은 사용자 컴퓨터의 설정에서 가져온 기본 통화 기호를 설정합니다. 앞에서 설명한 것처럼 이 설정이 잘못된 것일 수도 있습니다. .NET Framework 기본 설정을 사용하려면 응용 프로그램에서 false로 설정된 useUserOverride 매개 변수를 전달하여 CultureInfo 개체를 만들어야 합니다.

다음 예제에서는 앞의 예제와 유사한 코드를 사용합니다. 현재 문화권을 "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 Forms 응용 프로그램에서 실행하면 다음과 같은 출력이 표시됩니다.

100,00 F
100,00 €

많은 유럽 국가에서는 유로와 현지 통화를 공용으로 사용합니다. 응용 프로그램에서 두 통화를 모두 표시해야 하는 경우가 있을 수 있습니다. 다음 코드 예제에서는 기본 통화가 유로인 "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 샘플을 참조하십시오.

참고 항목

개념

다른 문화권의 형식 지정

기타 리소스

인코딩 및 지역화

형식 지정