A double is not an exact representation of a decimal number, it's an approximation, so you might not always get the result you want, just the closest result it can represent.

For numbers less than 100,000, you can easily determine the number of fractional digits you need:

`int fractional_digits = 5 - (int)Math.Floor(Math.Log10(number_to_round)) + 1;`

(Yes, you could use just 4 - ... instead, but I wrote it this way to make it more clear how it works.)`Math.Floor(Math.Log10(number_to_round))`

gives you one less than the number of digits the number has before the decimal place (it makes sense, if you think about how logs and digits work), subtract that from the total and you get the number of digits you want after the decimal point. This will even work for values < 1.I'm not sure of an obvious way to deal with numbers with more than 5 digits before the decimal point, maybe divide them by a power of 10 to make them shorter, then round, then multiply them by that power of 10 again?