In C# I want a function that rounds a given double to a given amount of decimals. I always want my function to return a value (which can be a string) with the given amount of decimals. If necessary, trailing zeros need to be added.
Example:
string result = MyRoundingFunction(1.01234567, 3);
// this must return "1.012"
That's easy, it's just rounding and converting to string. But here comes the problem:
string result2 = MyRoundingFuntion(1.01, 3);
// this must return "1.010"
Is there a convenient/standard way to do this, or do I manually need to add trailing zeros?
Any help is appreciated. Note that in the real life application I can't hard code the number of decimals.