3

I am currently making a tax program (study project) that read in a CSV file and generates a list of people and account balances, i need to round down 2 decimal places from one text box and add them up in another text box. my code attached tell me i cannot use " * " how do i times a decimal with 0.1? and if i am doing this wrong let me know, cheers!

public partial class Form1 : Form
{
    //CSV ARRAY LISTS
    List<string> fullName = new List<string>();
    List<string> accBalance = new List<string>();

    int currentItem;
    int index;
    int counter = 0;

    decimal interestBalance = 0;
    decimal result;

    decimal interestRemainder = 0;
    double round = 0.1;


 private void interestBalanceBox_TextChanged(object sender, EventArgs e)
    {
        interestRemainder = decimal.Parse(interestBalanceBox.Text);
        interestRemainder = Math.Truncate(0.1 * interestRemainder) / 100;
        interestRemainderBox.Text = interestRemainder.ToString();

    }

the program we are meant to represent is this one here! any help would be much appreciated.

enter image description here

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Getwrong
  • 177
  • 1
  • 2
  • 13
  • 2
    Decide whether you want to use `decimal` math or `double` math. `decimal * decimal => decimal` is well-defined, as is `double * double => double`. So when you've decided which you want to use: cast to the right type and use that. Anything involving finance is usually `decimal`. If the problem is simply the `0.1`, then: either multiply by `0.1M`, or just divide by `10` – Marc Gravell Dec 03 '17 at 01:21

1 Answers1

7
interestRemainder = Math.Truncate((decimal)0.1 * interestRemainder) / 100;

You need to cast the 0.1 to the same type as the other operand

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Chris Carr
  • 391
  • 2
  • 9
  • aw aha thanks man works now :D ill remember that for next time! cheers! – Getwrong Dec 03 '17 at 01:22
  • 3
    `0.1M` is probably simpler than `(decimal)0.1` – Marc Gravell Dec 03 '17 at 01:22
  • well the OP declares a variable called `round` they should use the variable in your example that's being cast as decimal vs the 0.1 value.. but your answer is correct.. or `roundM` `+1` for the correct answer – MethodMan Dec 03 '17 at 01:23
  • @MarcGravell `0.1M` would be duplicate... Not really sure if it is still not a good idea to create many post for *exactly the same* error message so. – Alexei Levenkov Dec 03 '17 at 01:24