Csharp/CSharp Tutorial/I18N Internationalization/Number Parse — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:14, 26 мая 2010

Parsing numbers based on Culture info

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
        CultureInfo current = CultureInfo.CurrentCulture;
        string numberString = "33,223.510";
        try
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
            try
            {
                double number = double.Parse(numberString);
                Console.WriteLine("Parsed! {0}", number);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception: {0}", e.ToString());
            }
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = current;
        }
    }
}

Parsing numbers with culture invariant parsing

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
    public static void Main()
    {
        CultureInfo current = CultureInfo.CurrentCulture;
        string numberString = "33,223.510";
            try
            {
                double number = double.Parse(numberString, CultureInfo.InvariantCulture);
                Console.WriteLine("Parsed! {0}", number);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception: {0}", e.ToString());
            }
    }
}
Parsed! 33223.51