使用英语阅读

通过


CultureInfo.NumberFormat 属性

定义

获取或设置 NumberFormatInfo,它定义适合区域性的、显示数字、货币和百分比的格式。

public virtual System.Globalization.NumberFormatInfo NumberFormat { get; set; }

属性值

一个 NumberFormatInfo,它定义适合区域性的、显示数字、货币和百分比的格式。

例外

该属性设置为 null。

设置了 NumberFormat 属性或任意 NumberFormatInfo 属性,但 CultureInfo 为只读。

示例

下面的代码示例显示 CultureInfo.Clone 还会克隆DateTimeFormatInfoCultureInfo关联的 和 NumberFormatInfo 实例。

using System;
using System.Globalization;

public class SamplesCultureInfo  {

   public static void Main()  {

      // Creates and initializes a CultureInfo.
      CultureInfo myCI = new CultureInfo("en-US", false);

      // Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      CultureInfo myCIclone = (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( "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 );
   }
}

/*
This code produces the following output.

DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
DTFI.AMDesignator       AM              a.m.
DTFI.DateSeparator      /               -
NFI.CurrencySymbol      $               USD
NFI.NumberDecimalDigits 2               4

*/

注解

你可以选择通过控制面板的区域和语言选项部分替代与 Windows 的当前区域性关联的某些值。 例如,用户可以选择以不同的格式显示日期,或者对区域性使用默认值以外的货币。

如果 UseUserOverridetrue ,并且指定的区域性与 Windows 的当前区域性匹配,则 CultureInfo 使用这些替代,包括 属性返回DateTimeFormatDateTimeFormatInfo实例属性的用户设置,以及 属性返回NumberFormatNumberFormatInfo实例的属性。 如果用户设置与 关联的 CultureInfo区域性不兼容,例如,如果所选日历不是 之 OptionalCalendars一,则方法的结果和属性的值是未定义的。

在用户访问 属性之前,NumberFormat不会计算 属性和 属性的值DateTimeFormat。 如果用户在应用程序运行时使用控制面板将当前区域性更改为新区域性,然后访问 DateTimeFormatNumberFormat 属性,则应用程序将检索新区域性的默认值。 不是原始区域性的替代。 若要保留原始当前区域性的替代,应用程序应在更改当前区域性之前访问 DateTimeFormatNumberFormat 属性。

适用于

另请参阅