How to limit a decimal number?
72,770
Solution 1
Math.Round Method (Decimal, Int32)
Example:
Math.Round(3.44, 1); //Returns 3.4.
Solution 2
I'm assuming you really mean formatting it for output:
Console.WriteLine("{0:0.###}", value);
Solution 3
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"
Solution 4
Use Math.Round
to round it to 3 decimal places.
Solution 5
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()
.
Author by
aharon
Updated on June 11, 2020Comments
-
aharon almost 4 years
Possible Duplicate:
How to format a decimalHow can I limit my decimal number so I'll get only 3 digits after the point?
e.g 2.774