Best way to convert string to decimal separator “.” and “,” insensitive way?

前端 未结 7 1142
北海茫月
北海茫月 2020-12-15 06:05

Application deals with strings that represent decimals that come from different cultures. For example \"1.1 and \"1,1\" is the same value.

I played with Deci

相关标签:
7条回答
  • 2020-12-15 06:26

    You can create a temporary CultureInfo object to use when you parse.

    // get a temporary culture (clone) to modify
    var ci = CultureInfo.InvariantCulture.Clone() as CultureInfo;
    ci.NumberFormat.NumberDecimalSeparator = ",";
    decimal number = decimal.Parse("1,1", ci); // 1.1
    
    0 讨论(0)
  • 2020-12-15 06:29

    I found another way to do it. It looks odd but it works fine for me.

    So if you don't know culture of the target system and you don't know which value you will get like 12.33 or 12,33 you can do following

    string amount = "12.33";
    // or i.e. string amount = "12,33";
    
    var c = System.Threading.Thread.CurrentThread.CurrentCulture;
    var s = c.NumberFormat.CurrencyDecimalSeparator;
    
    amount = amount.Replace(",", s);
    amount = amount.Replace(".", s);
    
    decimal transactionAmount = Convert.ToDecimal(amount); 
    
    0 讨论(0)
  • 2020-12-15 06:29

    Below is my implementation, any good idear?

    /// <summary>
    /// 
    /// </summary>
    public static class NumberExtensions
    {
        /// <summary>
        /// Convert string value to decimal ignore the culture.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>Decimal value.</returns>
        public static decimal ToDecimal ( this string value )
        {
            decimal number;
            string tempValue = value;
    
            var punctuation = value.Where ( x => char.IsPunctuation ( x ) ).Distinct ( );
            int count = punctuation.Count ( );
    
            NumberFormatInfo format = CultureInfo.InvariantCulture.NumberFormat;
            switch ( count )
            {
                case 0:
                    break;
                case 1:
                    tempValue = value.Replace ( ",", "." );
                    break;
                case 2:
                    if ( punctuation.ElementAt ( 0 ) == '.' )
                        tempValue = value.SwapChar ( '.', ',' );
                    break;
                default:
                    throw new InvalidCastException ( );
            }
    
            number = decimal.Parse ( tempValue, format );
            return number;
        }
        /// <summary>
        /// Swaps the char.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <returns></returns>
        public static string SwapChar ( this string value, char from, char to )
        {
            if ( value == null )
                throw new ArgumentNullException ( "value" );
    
            StringBuilder builder = new StringBuilder ( );
    
            foreach ( var item in value )
            {
                char c = item;
                if ( c == from )
                    c = to;
                else if ( c == to )
                    c = from;
    
                builder.Append ( c );
            }
            return builder.ToString ( );
        }
    }
    
    [TestClass]
    public class NumberTest
    {
    
        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void Convert_To_Decimal_Test ( )
        {
            string v1 = "123.4";
            string v2 = "123,4";
            string v3 = "1,234.5";
            string v4 = "1.234,5";
            string v5 = "123";
            string v6 = "1,234,567.89";
            string v7 = "1.234.567,89";
    
            decimal a1 = v1.ToDecimal ( );
            decimal a2 = v2.ToDecimal ( );
            decimal a3 = v3.ToDecimal ( );
            decimal a4 = v4.ToDecimal ( );
            decimal a5 = v5.ToDecimal ( );
            decimal a6 = v6.ToDecimal ( );
            decimal a7 = v7.ToDecimal ( );
    
            Assert.AreEqual ( ( decimal ) 123.4, a1 );
            Assert.AreEqual ( ( decimal ) 123.4, a2 );
            Assert.AreEqual ( ( decimal ) 1234.5, a3 );
            Assert.AreEqual ( ( decimal ) 1234.5, a4 );
            Assert.AreEqual ( ( decimal ) 123, a5 );
            Assert.AreEqual ( ( decimal ) 1234567.89, a6 );
            Assert.AreEqual ( ( decimal ) 1234567.89, a7 );
        }
        /// <summary>
        /// 
        /// </summary>
        [TestMethod]
        public void Swap_Char_Test ( )
        {
            string v6 = "1,234,567.89";
            string v7 = "1.234.567,89";
    
            string a1 = v6.SwapChar ( ',', '.' );
            string a2 = v7.SwapChar ( ',', '.' );
    
            Assert.AreEqual ( "1.234.567,89", a1 );
            Assert.AreEqual ( "1,234,567.89", a2 );
        }
    }
    
    0 讨论(0)
  • 2020-12-15 06:33

    Use TryParse twice with 2 styles that represent two possibilities
    If only one returns a value use that value If both return a value use the lesser value in absolute terms.

    TryParse will return 0 for numbers that use both grouping and decimal separator if you use the wrong style, but if you don't have a grouping separator in your string (e.g. a number is less than 1000) it will return value in both cases but the "wrong" number will be bigger (again in absolute terms)

    0 讨论(0)
  • 2020-12-15 06:39

    nice, still it's not 100% correct. when you use the case 1: you automatically suppose, that ',' stands for decimal digit. you should at least check if it occures more than once, cause in that case its a group separating symbol

                case 1:
                    var firstPunctuation = linq.ElementAt(0);
                    var firstPunctuationOccurence = value.Where(x => x == firstPunctuation).Count();
    
                    if (firstPunctuationOccurence == 1)
                    {
                        // we assume it's a decimal separator (and not a group separator)
                        value = value.Replace(firstPunctuation.ToString(), format.NumberDecimalSeparator);
                    }
                    else
                    {
                        // multiple occurence means that symbol is a group separator
                        value = value.Replace(firstPunctuation.ToString(), format.NumberGroupSeparator);
                    }
    
                    break;
    
    0 讨论(0)
  • 2020-12-15 06:45

    You just need to have the correct culture set, when calling Parse, like so:

    string s = "11,20";
    
    decimal c1 = decimal.Parse(s, new CultureInfo("fr-FR"));
    decimal c2 = decimal.Parse(s, new CultureInfo("en-AU"));
    
    Console.WriteLine(c1);
    Console.WriteLine(c2);
    
    0 讨论(0)
提交回复
热议问题