The easiest solution is just check if the value exceeds a boundary, and the divide it.
So if value is greater than 1m then divide by 1m and round the value. This also has the nice side effect that you know which suffix to put on it. It's important that you round the value, or you will get decimal points stretching off into infinity instead.
A downside to this approach is that you lose the fractional part of the value, but you can solve it with a slight adjustment. Try dividing by 0.1m, rounding the value then dividing by 10. This will give you the same value, but with 1 decimal place. You can do the same with 0.01 and 0.001 as well if you want more decimal places. Here's an example for you.