2016-04-26 47 views
-1

所以,我遇到了使用float的问题。当我去编译时,有一些特定的区域会告诉我一个错误:“不能显式地将类型'double'转换为'float'。存在明确的转换(你是否错过了一只猫?)问题是它只是在做它的三行代码(涉及remainingChange%0.25,remainingChange%0.10和remainingChange%0.05)我假设这与小数和精度有关,但我似乎无法弄清楚。解决它的任何指向我的方向是正确的建议,将不胜感激谢谢Float的问题

这里是适用的代码:

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     //Set up try catch for errors 
     try 
     { 
      //declare variables 
      float changeDue = 0; 
      float remainingChange = 0; 
      float purchaseAmount = 0; 
      float paymentAmount = 0; 

      //if statement to verify that the purchase amount is not empty 
      if (string.IsNullOrWhiteSpace(PurchaseAmount.Text)) 
      { 
       Error.Text = "The purhcase amount is empty"; 
       return; 
      } 

      //else-if statement to verify that the payment amount is not empty 
      else if (string.IsNullOrWhiteSpace(PaymentAmount.Text)) 
      { 
       Error.Text = "The submitted amount is empty"; 
       return; 
      } 

      //else-if statement to verify that the purchase and payment amounts are numeric 
      else if (!float.TryParse(PurchaseAmount.Text, out purchaseAmount)) 
      { 
       Error.Text = "The purchase amount will only take numbers"; 
       return; 
      } 

      else if (!float.TryParse(PaymentAmount.Text, out paymentAmount)) 
      { 
       Error.Text = "The submitted amount can only accept numbers"; 
       return; 
      } 

      //else-if statement to verify that the payment amount is greater than the purhcase amount 
      else if (purchaseAmount > paymentAmount) 
      { 
       Error.Text = "Submitted value can't be less than purchased value"; 
       return; 
      } 

      Error.Text = ""; 

      //Determine the change due and how it will be paid out, then write the values to the window 
      changeDue = paymentAmount - purchaseAmount; 

      remainingChange = changeDue % 100; 
      Hundreds.Text = ((changeDue/100) - (remainingChange/100)).ToString(); 

      Fifties.Text = ((remainingChange/50) - ((remainingChange % 50)/50)).ToString(); 
      remainingChange = remainingChange % 50; 

      Twenties.Text = ((remainingChange/20) - ((remainingChange % 20)/20)).ToString(); 
      remainingChange = remainingChange % 20; 

      Tens.Text = ((remainingChange/10) - ((remainingChange % 10)/10)).ToString(); 
      remainingChange = remainingChange % 10; 

      Fives.Text = ((remainingChange/5) - ((remainingChange % 5)/5)).ToString(); 
      remainingChange = remainingChange % 5; 

      Ones.Text = ((remainingChange/1) - ((remainingChange % 1)/1)).ToString(); 
      remainingChange = remainingChange % 1; 

      Quarters.Text = ((remainingChange/0.25) - ((remainingChange % 0.25)/0.25)).ToString(); 
      remainingChange = remainingChange % 0.25; 

      Dimes.Text = ((remainingChange/0.10) - ((remainingChange % 0.10)/0.10)).ToString(); 
      remainingChange = remainingChange % 0.10; 

      Nickels.Text = ((remainingChange/0.05) - ((remainingChange % 0.05)/0.05)).ToString(); 
      remainingChange = remainingChange % 0.05; 

      Pennies.Text = ((remainingChange/0.01) - ((remainingChange % 0.01)/0.01)).ToString(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("An error has occurred. The error was: " + ex.Message, "Error sample", MessageBoxButton.OK, MessageBoxImage.Warning); 
     } 
    } 

回答

2

当使用逐字数字,像0.05编译器默认为双Ť YPE。由于%运算符的一个操作数是双精度型,所以结果将作为double来处理。 您可以使用f后缀对这些浮点数进行注释,以便编译器知道您希望将这些数字用作浮点数(System.Single)而不是缺省双精度数。

试试这个:

remainingChange = remainingChange % 0.10f; //note the f suffix after the number 

不用通过这样做,你失去了一些精确的说,但我收集这不是你的情况的问题。

+0

这似乎解决了这个问题。我感谢您的帮助。 – MatthewSpire

1

如果其中一种浮点类型是double,则表达式的计算结果为双倍,这就是为什么您需要明确地转换为float

remainingChange = (float)(remainingChange % 0.05);