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

前端 未结 7 1169
北海茫月
北海茫月 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: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;
    

提交回复
热议问题