1

Based on this thread decimal vs double!, decimal is always used for money. What is the proper way to define percent? like TaxPercent? If it's double then for calculating amount * 8% (double) you would have to cast it. What's the proper way to define percent value (ie tax) and what would the calculation be.

Community
  • 1
  • 1
ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • Because percent is actually unit-less, I would think it depends on what you want to take the percentage of. If you want it from a money amount, then I would take the type of the amount, in casu decimal – Serge Desmedt Nov 18 '15 at 06:28

1 Answers1

4

Use the 'm' suffix to specify a literal as a decimal. So it must be 0.08m to ensure a double doesn't creep into the calculation.

 decimal tax = amount * 0.08m;

You'll find a list of valid suffix characters in this post.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • lets say you had to assign tax value of 8 to a variable: decimal taxPercent = (8 / 100); tax would now be 0 which is incorrect – ShaneKm Nov 18 '15 at 14:33
  • 1
    Don't write bad code, that's an integer division. You should now know to write `8m / 100m`. – Hans Passant Nov 18 '15 at 16:56