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,263
  • 11
  • 44
  • 77

3 Answers3

4

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
  • 429,027
  • 67
  • 649
  • 891
  • 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
  • 23,993
  • 9
  • 50
  • 53
1

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

Artyom
  • 3,337
  • 1
  • 31
  • 61