计算器:带十进制数的异常

时间:2014-03-22 15:05:56

标签: c# xaml decimal calculator

它可以用整数加,减,乘和除,但不能用十进制数加。 sum1变量有问题,就像它们的十进制符号格式错误一样。这是因为它们被解析为double,但我不知道如何以不同的方式编写它。

double sum1 = 0;
    double sum2 = 0;
    bool plusButtonClicked = false;
    bool subButtonClicked = false;
    bool multButtonClicked = false;
    bool divButtonClicked = false;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
    {
        Regex regex = new Regex("[^0-9]+");
        e.Handled = regex.IsMatch(e.Text);
    }

    private void one_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += one.Content;
    }

    private void two_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += two.Content;
    }

    private void three_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += three.Content;
    }

    private void four_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += four.Content;
    }

    private void five_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += five.Content;
    }

    private void six_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += six.Content;
    }

    private void seven_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += seven.Content;
    }

    private void eight_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += eight.Content;
    }

    private void nine_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += nine.Content;
    }

    private void zero_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Text += zero.Content;
    }

    private void add_Click(object sender, RoutedEventArgs e)
    {
        sum1 += double.Parse(numEnt.Text);
        numEnt.Clear();

        plusButtonClicked = true;
        subButtonClicked = false;
        multButtonClicked = false;
        divButtonClicked = false;
    }

    private void sub_Click(object sender, RoutedEventArgs e)
    {
        sum1 += double.Parse(numEnt.Text);
        numEnt.Clear();

        plusButtonClicked = false;
        subButtonClicked = true;
        multButtonClicked = false;
        divButtonClicked = false;
    }

    private void mult_Click(object sender, RoutedEventArgs e)
    {
        sum1 += double.Parse(numEnt.Text);
        numEnt.Clear();

        plusButtonClicked = false;
        subButtonClicked = false;
        multButtonClicked = true;
        divButtonClicked = false;
    }

    private void div_Click(object sender, RoutedEventArgs e)
    {
        sum1 += double.Parse(numEnt.Text);
        numEnt.Clear();

        plusButtonClicked = false;
        subButtonClicked = false;
        multButtonClicked = false;
        divButtonClicked = true;
    }

    private void deci_Click(object sender, RoutedEventArgs e)
    {
        string currentInput = numEnt.Text;
        bool pointFound = false;

        for (int i = 0; i < currentInput.Length; i++)
        {
            if (currentInput[i] == '.')
                pointFound = true;

        }

        if (pointFound == false)
            numEnt.Text += ".";
    }

    private void equal_Click(object sender, RoutedEventArgs e)
    {
        if (plusButtonClicked == true)
        {
            sum2 = sum1 + double.Parse(numEnt.Text);
            result.Text = sum2.ToString();
            sum1 = 0;
        }

        if (subButtonClicked == true)
        {
            sum2 = sum1 - double.Parse(numEnt.Text);
            result.Text = sum2.ToString();
            sum1 = 0;
        }

        if (multButtonClicked == true)
        {
            sum2 = sum1 * double.Parse(numEnt.Text);
            result.Text = sum2.ToString();
            sum1 = 0;
        }

        if (divButtonClicked == true)
        {
            sum2 = sum1 / double.Parse(numEnt.Text);
            result.Text = sum2.ToString();
            sum1 = 0;
        }
    }

    private void clear_Click(object sender, RoutedEventArgs e)
    {
        numEnt.Clear();
        calculations.Clear();
        result.Clear();
    }




}

2 个答案:

答案 0 :(得分:0)

首先将您的sum1变量声明为double。还有一件事需要for loop来检查字符串是否有decimal point。相反,您可以使用string.Contains方法。试试这段代码希望它会有所帮助

double sum1 =0.0 ;
private void add_Click(object sender, RoutedEventArgs e)
{
    sum1 += double.Parse(numEnt.Text);
    numEnt.Clear();

    plusButtonClicked = true;
    subButtonClicked = false;
    multButtonClicked = false;
    divButtonClicked = false;
}

private void sub_Click(object sender, RoutedEventArgs e)
{
    sum1 -= double.Parse(numEnt.Text);
    numEnt.Clear();

    plusButtonClicked = false;
    subButtonClicked = true;
    multButtonClicked = false;
    divButtonClicked = false;
}

private void mult_Click(object sender, RoutedEventArgs e)
{
    sum1 *= double.Parse(numEnt.Text);
    numEnt.Clear();

    plusButtonClicked = false;
    subButtonClicked = false;
    multButtonClicked = true;
    divButtonClicked = false;
}

private void div_Click(object sender, RoutedEventArgs e)
{
    sum1 /= double.Parse(numEnt.Text);
    numEnt.Clear();

    plusButtonClicked = false;
    subButtonClicked = false;
    multButtonClicked = false;
    divButtonClicked = true;
}

private void deci_Click(object sender, RoutedEventArgs e)
{
    string currentInput = numEnt.Text;
    bool pointFound = false;

    //for (int i = 0; i < currentInput.Length; i++)
    //{
        if(currentInput.Contains("."))//if (currentInput[i] == '.')
            pointFound = true;

    //}

    if (pointFound == false)
        numEnt.Text += ".";
}

答案 1 :(得分:0)

可能dot不是您的小数点分隔符。在这种情况下,您可以使用

 double.Parse(string, IFormatProvider) 

设置所需的小数分隔符。

试试这个

var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
var numberFormat = (System.Globalization.NumberFormatInfo) currentCultur .NumberFormat.Clone();
numberFormat .NumberDecimalSeparator = "."; 

sum1 + = double.Parse(numEnt.Text,numberFormat);