0

A follow on from this question: C# how to always round down to nearest 50

How would I round a decimal to the nearest 50 and return an int, I could do the following but there must be a more efficent solution?

decimal test = 154.45m;
decimal newValue = Math.Floor(test / 50m) * 50.0m;
int testInt = Convert.ToInt32(newValue);
Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190

1 Answers1

5

Instead of converting to int, you can just get the int part by casting.

int testInt = (int) Math.Floor(test / 50m) * 50.0m;

I am not sure how much performance you would gain by this, but Convert.ToInt32 would also do the rounding to nearest 32 bit integer, whereas casting would just give you the int part

Habib
  • 219,104
  • 29
  • 407
  • 436