当前位置: 首页>>代码示例>>C#>>正文


C# NumberFormatInfo类代码示例

本文整理汇总了C#中System.Globalization.NumberFormatInfo的典型用法代码示例。如果您正苦于以下问题:C# NumberFormatInfo类的具体用法?C# NumberFormatInfo怎么用?C# NumberFormatInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


NumberFormatInfo类属于System.Globalization命名空间,在下文中一共展示了NumberFormatInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Globalization;
using System.Text;

public sealed class App 
{
    static void Main() 
    {
        StringBuilder sb = new StringBuilder();

        // Loop through all the specific cultures known to the CLR.
        foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
        {
            // Only show the currency symbols for cultures that speak English.
            if (ci.TwoLetterISOLanguageName != "en") continue;
             
            // Display the culture name and currency symbol.
            NumberFormatInfo nfi = ci.NumberFormat;
            sb.AppendFormat("The currency symbol for '{0}' is '{1}'",
                ci.DisplayName, nfi.CurrencySymbol);
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
    }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:26,代码来源:NumberFormatInfo

输出:

The currency symbol for 'English (United States)' is '$'
The currency symbol for 'English (United Kingdom)' is '£'
The currency symbol for 'English (Australia)' is '$'
The currency symbol for 'English (Canada)' is '$'
The currency symbol for 'English (New Zealand)' is '$'
The currency symbol for 'English (Ireland)' is '?'
The currency symbol for 'English (South Africa)' is 'R'
The currency symbol for 'English (Jamaica)' is 'J$'
The currency symbol for 'English (Caribbean)' is '$'
The currency symbol for 'English (Belize)' is 'BZ$'
The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
The currency symbol for 'English (Zimbabwe)' is 'Z$'
The currency symbol for 'English (Republic of the Philippines)' is 'Php'

示例2: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberFormatInfo current1 = CultureInfo.CurrentCulture.NumberFormat;
      Console.WriteLine(current1.IsReadOnly);
      
      NumberFormatInfo current2 = NumberFormatInfo.CurrentInfo;
      Console.WriteLine(current2.IsReadOnly);
      
      NumberFormatInfo current3 = NumberFormatInfo.GetInstance(CultureInfo.CurrentCulture);
      Console.WriteLine(current3.IsReadOnly);
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:18,代码来源:NumberFormatInfo

输出:

True
True
True

示例3: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberFormatInfo current1 = NumberFormatInfo.CurrentInfo;
      current1 = (NumberFormatInfo) current1.Clone();
      Console.WriteLine(current1.IsReadOnly);

      CultureInfo culture2 = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
      NumberFormatInfo current2 = culture2.NumberFormat;
      Console.WriteLine(current2.IsReadOnly);
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:17,代码来源:NumberFormatInfo

输出:

False
False

示例4: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture;
      NumberFormatInfo nfi;
      
      culture = CultureInfo.CurrentCulture;
      nfi = culture.NumberFormat;
      Console.WriteLine("Culture Name:    {0}", culture.Name);
      Console.WriteLine("User Overrides:  {0}", culture.UseUserOverride);
      Console.WriteLine("Currency Symbol: {0}\n", culture.NumberFormat.CurrencySymbol);
            
      culture = new CultureInfo(CultureInfo.CurrentCulture.Name, false);
      Console.WriteLine("Culture Name:    {0}", culture.Name);
      Console.WriteLine("User Overrides:  {0}", culture.UseUserOverride);
      Console.WriteLine("Currency Symbol: {0}", culture.NumberFormat.CurrencySymbol);
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:23,代码来源:NumberFormatInfo

输出:

Culture Name:    en-US
User Overrides:  True
Currency Symbol: USD

Culture Name:    en-US
User Overrides:  False
Currency Symbol: $

示例5: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberFormatInfo nfi;
      
      nfi = System.Globalization.NumberFormatInfo.InvariantInfo;
      Console.WriteLine(nfi.IsReadOnly);               
      
      nfi = CultureInfo.InvariantCulture.NumberFormat;
      Console.WriteLine(nfi.IsReadOnly);               
      
      nfi = new NumberFormatInfo();
      Console.WriteLine(nfi.IsReadOnly);               
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:20,代码来源:NumberFormatInfo

输出:

True
True
False

示例6: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture;
      NumberFormatInfo nfi;
      
      nfi = CultureInfo.GetCultureInfo("id-ID").NumberFormat;
      Console.WriteLine("Read-only: {0}", nfi.IsReadOnly);
      
      culture = new CultureInfo("id-ID");
      nfi = NumberFormatInfo.GetInstance(culture);
      Console.WriteLine("Read-only: {0}", nfi.IsReadOnly);
      
      culture = CultureInfo.CreateSpecificCulture("id-ID");
      nfi = culture.NumberFormat;
      Console.WriteLine("Read-only: {0}", nfi.IsReadOnly);
      
      culture = new CultureInfo("id-ID");
      nfi = culture.NumberFormat;
      Console.WriteLine("Read-only: {0}", nfi.IsReadOnly);
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:27,代码来源:NumberFormatInfo

输出:

Read-only: True
Read-only: False
Read-only: False
Read-only: False

示例7: Main

//引入命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Get all the neutral cultures
      List<String> names = new List<String>();
      Array.ForEach(CultureInfo.GetCultures(CultureTypes.NeutralCultures),
                    culture => names.Add(culture.Name));
      names.Sort();
      foreach (var name in names) {
         // Ignore the invariant culture.
         if (name == "") continue;
         
         ListSimilarChildCultures(name);        
      }
   }

   private static void ListSimilarChildCultures(string name)
   { 
      // Create the neutral NumberFormatInfo object.
      NumberFormatInfo nfi = CultureInfo.GetCultureInfo(name).NumberFormat;
      // Retrieve all specific cultures of the neutral culture.
      CultureInfo[] cultures = Array.FindAll(CultureInfo.GetCultures(CultureTypes.SpecificCultures), 
                               culture => culture.Name.StartsWith(name + "-", StringComparison.OrdinalIgnoreCase));
      // Create an array of NumberFormatInfo properties
      PropertyInfo[] properties = typeof(NumberFormatInfo).GetProperties(BindingFlags.Instance | BindingFlags.Public);
      bool hasOneMatch = false;

      foreach (var ci in cultures) {
         bool match = true;     
         // Get the NumberFormatInfo for a specific culture.
         NumberFormatInfo specificNfi = ci.NumberFormat;
         // Compare the property values of the two.
         foreach (var prop in properties) {
            // We're not interested in the value of IsReadOnly.     
            if (prop.Name == "IsReadOnly") continue;
            
            // For arrays, iterate the individual elements to see if they are the same.
            if (prop.PropertyType.IsArray) { 
               IList nList = (IList) prop.GetValue(nfi, null);
               IList sList = (IList) prop.GetValue(specificNfi, null);
               if (nList.Count != sList.Count) {
                  match = false;
                  break;
               } 

               for (int ctr = 0; ctr < nList.Count; ctr++) {
                  if (! nList[ctr].Equals(sList[ctr])) {
                     match = false;
                     break;
                  }     
               }
            }   
            else if (! prop.GetValue(specificNfi).Equals(prop.GetValue(nfi))) {
               match = false;
               break;   
            }        
         }
         if (match) {
            Console.WriteLine("NumberFormatInfo object for '{0}' matches '{1}'", 
                                      name, ci.Name);
            hasOneMatch = true;
         }                                       
      }
      if (! hasOneMatch)
         Console.WriteLine("NumberFormatInfo object for '{0}' --> No Match", name);            

      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:77,代码来源:NumberFormatInfo

示例8: GetFormat

//引入命名空间
using System;
using System.Globalization;

public class CurrentCultureFormatProvider : IFormatProvider
{
   public Object GetFormat(Type formatType) 
   {
      Console.WriteLine("Requesting an object of type {0}", 
                        formatType.Name);
      if (formatType == typeof(NumberFormatInfo))
         return NumberFormatInfo.CurrentInfo;
      else if (formatType == typeof(DateTimeFormatInfo))
         return DateTimeFormatInfo.CurrentInfo;
      else
         return null;
   }
}

public class Example
{
   public static void Main()
   {
      Decimal amount = 1203.541m;
      string value = amount.ToString("C2", new CurrentCultureFormatProvider());
      Console.WriteLine(value);
      Console.WriteLine();
      string composite = String.Format(new CurrentCultureFormatProvider(), 
                                       "Date: {0}   Amount: {1}   Description: {2}",
                                       DateTime.Now, 1264.03m, "Service Charge");
      Console.WriteLine(composite);
      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:34,代码来源:NumberFormatInfo

输出:

Requesting an object of type NumberFormatInfo
$1,203.54

Requesting an object of type ICustomFormatter
Requesting an object of type DateTimeFormatInfo
Requesting an object of type NumberFormatInfo
Date: 11/15/2012 2:00:01 PM   Amount: 1264.03   Description: Service Charge

示例9: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] formatStrings = { "C2", "E1", "F", "G3", "N", 
                                 "#,##0.000", "0,000,000,000.0##" };
      CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
      Decimal[] values = { 1345.6538m, 1921651.16m };
      
      foreach (var value in values) {
         foreach (var formatString in formatStrings) {
            string resultString = value.ToString(formatString, culture);
            Console.WriteLine("{0,-18} -->  {1}", formatString, resultString);
         }
         Console.WriteLine();      
      }   
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:22,代码来源:NumberFormatInfo

输出:

C2                 -->  $1,345.65
E1                 -->  1.3E+003
F                  -->  1345.65
G3                 -->  1.35E+03
N                  -->  1,345.65
#,##0.000          -->  1,345.654
0,000,000,000.0##  -->  0,000,001,345.654

C2                 -->  $1,921,651.16
E1                 -->  1.9E+006
F                  -->  1921651.16
G3                 -->  1.92E+06
N                  -->  1,921,651.16
#,##0.000          -->  1,921,651.160
0,000,000,000.0##  -->  0,001,921,651.16

示例10: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Decimal[] values = { 1345.6538m, 1921651.16m };
      
      foreach (var value in values) {
         string resultString = value.ToString();
         Console.WriteLine(resultString);
         Console.WriteLine();      
      }   
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:16,代码来源:NumberFormatInfo

输出:

1345.6538

1921651.16

示例11: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Retrieve a writable NumberFormatInfo object.
      CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
      NumberFormatInfo nfi = enUS.NumberFormat;

      // Use the ISO currency symbol instead of the native currency symbol.
      nfi.CurrencySymbol =  (new RegionInfo(enUS.Name)).ISOCurrencySymbol;
      // Change the positive currency pattern to <code><space><value>.
      nfi.CurrencyPositivePattern = 2;
      // Change the negative currency pattern to <code><space><sign><value>.     
      nfi.CurrencyNegativePattern = 12;
      
      // Produce the result strings by calling ToString.
      Decimal[] values = { 1065.23m, 19.89m, -.03m, -175902.32m };
      foreach (var value in values)
         Console.WriteLine(value.ToString("C", enUS));

      Console.WriteLine();
      
      // Produce the result strings by calling a composite formatting method.
      foreach (var value in values)
         Console.WriteLine(String.Format(enUS, "{0:C}", value));      
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:31,代码来源:NumberFormatInfo

输出:

USD 1,065.23
USD 19.89
USD -0.03
USD -175,902.32

USD 1,065.23
USD 19.89
USD -0.03
USD -175,902.32

示例12: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Instantiate a read-only NumberFormatInfo object.
      CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
      NumberFormatInfo nfi = enUS.NumberFormat;

      // Modify the relevant properties.
      nfi.NumberGroupSeparator = "-";
      nfi.NumberGroupSizes = new int[] { 3, 2, 4};
      nfi.NumberDecimalDigits = 0;
      
      int[] ids = { 111223333, 999776666 };
      
      // Produce the result string by calling ToString.
      foreach (var id in ids)
         Console.WriteLine(id.ToString("N", enUS));

      Console.WriteLine();
      
      // Produce the result string using composite formatting.
      foreach (var id in ids)
         Console.WriteLine(String.Format(enUS, "{0:N}", id));
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:30,代码来源:NumberFormatInfo

输出:

1112-23-333
9997-76-666

1112-23-333
9997-76-666

示例13: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      String[] values = { "1,034,562.91", "9 532 978,07" };
      String[] cultureNames = { "en-US", "fr-FR", "" };
      
      foreach (var value in values) {
         foreach (var cultureName in cultureNames) {
            CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
            String name = culture.Name == "" ? "Invariant" : culture.Name;
            try {
               Decimal amount = Decimal.Parse(value, culture);
               Console.WriteLine("'{0}' --> {1} ({2})", value, amount, name);
            }
            catch (FormatException) {
               Console.WriteLine("'{0}': FormatException ({1})",
                                 value, name);
            }   
         }
         Console.WriteLine();
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:28,代码来源:NumberFormatInfo

输出:

'1,034,562.91' --> 1034562.91 (en-US)
'1,034,562.91': FormatException (fr-FR)
'1,034,562.91' --> 1034562.91 (Invariant)

'9 532 978,07': FormatException (en-US)
'9 532 978,07' --> 9532978.07 (fr-FR)
'9 532 978,07': FormatException (Invariant)

示例14: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo stdCulture = CultureInfo.GetCultureInfo("en-US");
      CultureInfo custCulture = CultureInfo.CreateSpecificCulture("en-US"); 
            
      String value = "310,16";
      try {
         Console.WriteLine("{0} culture reflects user overrides: {1}", 
                           stdCulture.Name, stdCulture.UseUserOverride);
         Decimal amount = Decimal.Parse(value, stdCulture);
         Console.WriteLine("'{0}' --> {1}", value, amount.ToString(CultureInfo.InvariantCulture));                                                                                        
      }
      catch (FormatException) {
         Console.WriteLine("Unable to parse '{0}'", value);
      }    
      Console.WriteLine();
                                            
      try {
         Console.WriteLine("{0} culture reflects user overrides: {1}", 
                           custCulture.Name, custCulture.UseUserOverride);
         Decimal amount = Decimal.Parse(value, custCulture);
         Console.WriteLine("'{0}' --> {1}", value, amount.ToString(CultureInfo.InvariantCulture));                                                                                        
      }
      catch (FormatException) {
         Console.WriteLine("Unable to parse '{0}'", value);
      }   
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:34,代码来源:NumberFormatInfo

输出:

en-US culture reflects user overrides: False
'310,16' --> 31016

en-US culture reflects user overrides: True
'310,16' --> 310.16

示例15: Main

//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
      PersistData();
      
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
      RestoreData();
   }

   private static void PersistData()
   {
      // Define an array of floating-point values.
      Double[] values = { 160325.972, 8631.16, 1.304e5, 98017554.385, 
                          8.5938287084321676e94 };
      Console.WriteLine("Original values: ");
      foreach (var value in values) 
         Console.WriteLine(value.ToString("R", CultureInfo.InvariantCulture));
         
      // Serialize an array of doubles to a file 
      StreamWriter sw = new StreamWriter(@".\NumericData.bin");
      for (int ctr = 0; ctr < values.Length; ctr++) {
         sw.Write(values[ctr].ToString("R"));
         if (ctr < values.Length - 1) sw.Write("|");
      }
      sw.Close();
      Console.WriteLine();
   }
   
   private static void RestoreData()
   {   
      // Deserialize the data
      StreamReader sr = new StreamReader(@".\NumericData.bin");
      String data = sr.ReadToEnd();
      sr.Close();
      
      String[] stringValues = data.Split('|');
      List<Double> newValueList = new List<Double>();
      
      foreach (var stringValue in stringValues) {
         try {
            newValueList.Add(Double.Parse(stringValue));
         }
         catch (FormatException) {
            newValueList.Add(Double.NaN);
         }   
      }                                   

      Console.WriteLine("Restored values:");
      foreach (var newValue in newValueList) 
         Console.WriteLine(newValue.ToString("R", NumberFormatInfo.InvariantInfo));
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:61,代码来源:NumberFormatInfo

输出:

Original values:
160325.972
8631.16
130400
98017554.385
8.5938287084321671E+94

Restored values:
160325972
863116
130400
98017554385
8.5938287084321666E+110


注:本文中的System.Globalization.NumberFormatInfo类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。