我如何才能使“点”和“逗号”字符在TextBox中以相同的方式起作用而又不出错?

时间:2019-01-24 16:13:44

标签: c#

我要打“。”和“,”都可以用作十进制数字。我的问题是,在我的计算机上,默认情况下它是“。”(23.33可以正常工作),当我尝试将“,”(23,33)放入时它会出错。我该如何运作。

private static Double SolveExpression(String expression)
{
    char uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator[0];
    expression = expression.Replace('.', uiSep);
    expression = expression.Replace(',', uiSep);
    if (expression.StartsWith("("))
    {
        int opening_brackets = 1, closing_brackets = 0, current_symbol = 1;
        while (opening_brackets != closing_brackets)
        {
            if (expression[current_symbol] == '(')
                opening_brackets++;
            else if (expression[current_symbol] == ')')
                closing_brackets++;

            current_symbol++;
        }
        String expr = expression.Substring(1, current_symbol - 2);
        expression = expression.Remove(0, current_symbol);

        Match operation = Regex.Match(expression, @"^[\+\-\*\/]");
        if (operation.Success)
        {
            expression = expression.Remove(0, operation.Value.Length);
            switch (operation.Value)
            {
                case "+":
                    {
                        return SolveExpression(expr) + SolveExpression(expression);
                    }
                case "-":
                    {
                        return SolveExpression(expr) - SolveExpression(expression);
                    }
                case "*":
                    {
                        return SolveExpression(expr) * SolveExpression(expression);
                    }
                case "/":
                    {
                        return SolveExpression(expr) / SolveExpression(expression);
                    }
            }
        }
        else
            return SolveExpression(expr);
    }

    Match constant = Regex.Match(expression, @"(^-*\d+)((\.|\,)(\d+))?");
    if (constant.Success)
    {
        expression = expression.Remove(0, constant.Value.Length);

        Match operation = Regex.Match(expression, @"^[\+\-\*\/]");
        if (operation.Success)
        {
            expression = expression.Remove(0, operation.Value.Length);
            switch (operation.Value)
            {
                case "+":
                    {
                        return Double.Parse(constant.Value) + SolveExpression(expression);
                    }
                case "-":
                    {
                        return Double.Parse(constant.Value) - SolveExpression(expression);
                    }
                case "*":
                    {
                        return Double.Parse(constant.Value) * SolveExpression(expression);
                    }
                case "/":
                    {
                        return Double.Parse(constant.Value) / SolveExpression(expression);
                    }
            }
        }
        else
            return Double.Parse(constant.Value);
    }
    else
        //throw new Exception("Invalid Expression");
        MessageBox.Show("You have entered invalid expression! Revise and try again", "Something went wrong", MessageBoxButtons.OK,MessageBoxIcon.Error);

    return 0;
}

4 个答案:

答案 0 :(得分:0)

没有完全明白你的意思。但是以下内容可以帮助处理字符串的数字格式:

double [] value = {123.456,567.1};
Console.WriteLine("Your account balance is {0:C2}.", value[0],value[1]);
Console.WriteLine("Your account balance is {1:C3}.", value[0],value[1]);
// Displays "Your account balance is $123.46."
        //  "Your account balance is $567.100."

其中:0和1是值数组的元素,值[0]和值1。 C2和C3以货币格式显示,十进制后两位和三位数字

有关详细信息和更多信息,请查看this

答案 1 :(得分:0)

如果您希望将任一字符作为小数点,但不接受千位分隔符,那么最简单的方法是将所有,替换为{{ 1}},并使用.

CultureInfo.InvariantCulture

但是,如果用户提供带有千位分隔符(带或不带小数)的值,则此方法将给出错误的结果。

答案 2 :(得分:0)

在以下行中使用正则表达式:

\-?[0-9]+[\.\,][0-9]+

这将允许以下形式的数字:

  • 12.34
  • 12,34
  • -12.34
  • -12,34

然后使用string.Replace 可以这样做:

input = input.Replace(",", ".")
var numberFormat = System.Globalization.CultureInfo.InvariantCulture.NumberFormat;
double value = Convert.ToDouble(TextBox.Text, numberFormat);

可以解析数字。

答案 3 :(得分:0)

尝试一下:

var str = "1.25";
var str2 = "1,25";

double Parse(string txt){
    return double.Parse(txt.Replace(",",".")
                           .Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
}

Parse(str); // not crashing
Parse(str2); // not crashing