Convert type string <--> decimal

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Test { class Program {

    private static decimal ToDecimal(string Value)
    {
        if (string.IsNullOrEmpty(Value))
            return 0;
        else
        {

            try
            {
                //sử dụng dạng 1,123.45
                return Decimal.Parse(Value.Replace(" ", ""), NumberStyles.AllowThousands | 
                                        NumberStyles.AllowDecimalPoint | 
                                        NumberStyles.AllowCurrencySymbol,                        
                                        CultureInfo.CreateSpecificCulture("en-us"));
            }
            catch
            {
                try
                {
                    //sử dụng dạng 1.123,45
                    return Decimal.Parse(Value.Replace(" ", ""), NumberStyles.AllowThousands |
                                            NumberStyles.AllowDecimalPoint |
                                            NumberStyles.AllowCurrencySymbol,
                                            CultureInfo.CreateSpecificCulture("nl-NL"));
                }
                catch {
                    // c&ograve;n nếu m&agrave; kh&ocirc;ng được th&igrave; đ&oacute; kh&ocirc;ng phải l&agrave; chuỗi số
                    return 0;
                }
            }

        }
    }
    private static string fDecToStr(decimal dec)
    {
        return fDecToStr(dec, ",", 0, string.Empty);
    }
    private static string fDecToStr(decimal dec, string strDauHangTram)
    {
        return fDecToStr(dec, strDauHangTram, 0, string.Empty);
    }
    private static string fDecToStr(decimal dec, string strDauHangTram, int intDauThapPhan)
    {
        return  fDecToStr(dec, strDauHangTram, intDauThapPhan , string.Empty);
    }

    private static string fDecToStr(decimal dec, string strDauHangTram, int intDauThapPhan , string dtrHauTo)
    {

        string strFormat;
        strFormat = "#,###";
        for (int i = 0; i &lt; intDauThapPhan; i++)
        {
            if (strFormat.EndsWith("#,###"))
                strFormat += ".";
            strFormat += "#";

        }


        //sử dụng dạng 1,123
        if (strDauHangTram.Equals(","))
            return dec.ToString(strFormat, CultureInfo.CreateSpecificCulture("en-us"));

        return dec.ToString(strFormat, CultureInfo.CreateSpecificCulture("nl-NL"));

    }
    private static string fIntToStr(int Value)
    {
        return fIntToStr(Value, ",");
    }
    private static string fIntToStr(int Value, string strDauHangTram)
    {
        //sử dụng dạng 1,123
        if (strDauHangTram.Equals(","))
            return Value.ToString("#,###.", CultureInfo.CreateSpecificCulture("en-us"));

        return Value.ToString("#,###.", CultureInfo.CreateSpecificCulture("nl-NL"));

    }


    static void Main(string[] args)
    {
        string a;
        a = "1115.45.4545,25201";
        decimal dec;
        dec = ToDecimal(a);


        Console.WriteLine(dec.ToString("#,###", CultureInfo.CreateSpecificCulture("nl-NL")));
        Console.WriteLine(dec.ToString("#,###", CultureInfo.CreateSpecificCulture("en-us")));

        Console.WriteLine("===============");
        Console.WriteLine(fDecToStr(dec, ".", 3, string.Empty));
        Console.WriteLine(fDecToStr(dec, ",", 5, string.Empty));

        Console.WriteLine("===============");
    }
}

}