For the first one, if you don't know how many the digits the variable could be, you can have a extension method:
public static string ToSpecificFormat(this decimal value)
{
var count = BitConverter.GetBytes(decimal.GetBits(value)[3])[2];
return value.ToString(count == 0 ? "N1" : "N" + count);
}
This will make sure there is at least 1 digit in the output.
For the second one, the selected answer will fail if the number > 1000000000. This one should work:
public static string ToFixedLength(this decimal value)
{
if (value >= 1000000000m) return value.ToString("N0");
var format = 9 - Math.Floor(value).ToString().Length;
return value.ToString("N" + format);
}
output:
1.234m.ToFixedLength(); // 1.23400000
101m.ToFixedLength(); // 101.000000
123456789123m.ToFixedLength(); // 123,456,789,123