1

I need to divide a USD amount by a number of months for a payment schedule, and I want to be able to get the remainder of the division if the money doesn't divide cleanly. Normally I would just use a modulus operation to get the remainder but I need to also account for the hundredths place, which modulus doesn't do. $500.00 divvied up over 8 months is a perfectly valid amount at $62.50 but a mod operation will return $4.00.

I'm currently using the decimal type for my currency operations, is there a better way to do what I'm doing or should I just go about and write a function that does this?

Fam
  • 580
  • 1
  • 7
  • 29
  • Show your calculations - yes `decimal` is the perfect type to use for monetary calculations. – D Stanley Apr 16 '16 at 16:57
  • ((value * 100) mod 8 ) / 100 ? – Ian Mercer Apr 16 '16 at 16:57
  • Add code to your question, not comments - and `value` is a `decimal`? – D Stanley Apr 16 '16 at 16:58
  • Also there's no `mod` operator in C#, In C# it's `%`. Please show your _exact_ C# code along with relevant declarations. – D Stanley Apr 16 '16 at 16:59
  • @IanMercer, I can't believe it was as simple as multiplying by a hundred before the mod operation and then dividing again by a hundred to get the true result. Your solution worked perfectly for me. If you'd like, please post this as the answer and I will accept it. – Fam Apr 16 '16 at 17:04

1 Answers1

1

You can calculate the remainder in cents by multiplying up, then applying mod and then dividing back down:

((value * 100) % 8 ) / 100 
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133