Possible Duplicate:
How to format a decimal
How can I limit my decimal number so I'll get only 3 digits after the point?
e.g 2.774
Possible Duplicate:
How to format a decimal
How can I limit my decimal number so I'll get only 3 digits after the point?
e.g 2.774
Limiting the precision of a floating point number is a SQL concept. Decimal in csharp only means that it will remember the precision assigned. You can round to three decimal places before assigning. IE, Math.Round().
I'm assuming you really mean formatting it for output:
Console.WriteLine("{0:0.###}", value);
To get Decimal back use Math.Round with Second parameter specifying number of decimal points.
decimal d = 54.9700M;
decimal f = (Math.Round(d, 2)); // 54.97
To Get String representation of number use .ToString() Specifiying Decimal Points as N3. Where 3 is the decimal points
decimal d = 54.9700M;
string s = number.ToString("N3"); // "54.97"
Part of my answer is the response, another part is just a interesting point:
I often want to see the variable as a prop/field. So a create a extension method to solve my problem:
*Tensao is just an Enum that have a value related.
public static class TensaoExtensions {
public static double TensaoNominal(this Tensao tensao) {
return Math.Round((double.Parse(EnumMapper.Convert(typeof(Tensao), tensao.ToString()))) * 1000 / Math.Sqrt(3), 3);
}
}