Round Off decimal values in C#
64,514
Solution 1
Look at Math.Round(decimal)
or the overload which takes a MidpointRounding
argument.
Of course, you'll need to parse and format the value to get it from/to text. If this is input entered by the user, you should probably use decimal.TryParse
, using the return value to determine whether or not the input was valid.
string text = "19500.55";
decimal value;
if (decimal.TryParse(text, out value))
{
value = Math.Round(value);
text = value.ToString();
// Do something with the new text value
}
else
{
// Tell the user their input is invalid
}
Solution 2
Math.Round( value, 0 )
Solution 3
Try this...
var someValue=123123.234324243m;
var strValue=someValue.ToString("#");
Author by
Guddu
Updated on December 18, 2020Comments
-
Guddu over 3 years
how do i round off decimal values ?
Example :decimal Value = " 19500.98"
i need to display this value to textbox with rounded off like " 19501 "
if decimal value = " 19500.43"
then
value = " 19500 "
-
Ivar over 8 yearsCeiling rounds it up.
19500.43
will become19501
, and not19500
like the OP intended. -
Bilal over 8 yearsyes so if you want to round off amount if it is greater that 0.5 then u can use Math.Round(Value, MidpointRounding.AwayFromZero);
-
E. Zeytinci over 4 yearsPlease explain your answer.
-
Arun Vinoth - MVP over 4 yearsWhile this code may provide a solution to OP's problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution.