0

Is there a way of checking that a value is wholly divisible by another number, for example 1000 divided by 100 would be true, but 1115 divided by 100 would be false?

I am tring to

Any help would be much appreciated :-)

iggyweb
  • 2,373
  • 12
  • 47
  • 77

3 Answers3

5

You can use the %-operator:

bool isDivisible = 1115 % 100 == 0;

The % operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 2
    It's amazing that [no-one has asked this before](http://stackoverflow.com/questions/19395334/check-if-number-is-divisible-by-24) – Rawling Jun 15 '15 at 09:53
  • 1
    And it's amazing that a programmer like OP never heard of modulo before. Cause otherwise you'd just have to type "modulo c#" into google. – Dolgsthrasir Jun 15 '15 at 09:58
  • Thank you Tim, solved my problem immediately :-) – iggyweb Jun 15 '15 at 10:59
1

You can use mod operator (%) and check that remainder is equal to 0:

var result = (1000.00 % 100) == 0; // evaluates to true
var result = (1115.00 % 100) == 0; // evaluates to false
dotnetom
  • 24,551
  • 9
  • 51
  • 54
1

Check out % operator. 1000 % 100 yields 0. 1115 % 100 yields 15

Artyom
  • 3,507
  • 2
  • 34
  • 67