We can use ToString("F4"))
to get four decimal point in decimal datatype.
decimal test= 1234.123456789;
Console.WriteLine(test.ToString("F4"));
Is there a way just to specify precision of decimal point globally?
We can use ToString("F4"))
to get four decimal point in decimal datatype.
decimal test= 1234.123456789;
Console.WriteLine(test.ToString("F4"));
Is there a way just to specify precision of decimal point globally?
You could create an extension method:
public static class DecimalExtensions
{
public static string ToString4Points(this decimal d)
{
return d.ToString("F4");
}
}
...and call it as:
decimal d = 1234.123456789m;
var str = d.ToString4Points();
You can use an extension.
On this way it works:
public class Program
{
public static void Main()
{
decimal t = 1.0M;
Console.WriteLine(t.ToStringD(2));
Console.WriteLine(t.ToStringD());
}
}
public static class Extensions
{
private const int DEFAULT_PRECISION = 4;
public static string ToStringD(this decimal value, int? precision = null)
{
return value.ToString("F" + (precision ?? DEFAULT_PRECISION).ToString());
}
}