Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I need to round a value in c#.

Example:

decimal val=4.1,4.7

I need the result is 5


[Edit]Removed shouting[/Edit]
Posted
Updated 31-May-12 4:10am
v2

Hi,

Math.Round((int)Value);
try it

or
 
Share this answer
 
Comments
Maciej Los 31-May-12 9:55am    
.. or what? ')
lukeer 31-May-12 10:16am    
The cast to int acts like a Floor() method for positive values. For negative values, it's like Ceil().

Anyway, there's no point in rounding its return value.
Use Math.Round(DecimalValue, MidpointRounding.AwayFromZero)
 
Share this answer
 
Use Math.Round[^] or Math.Ceiling[^] or Math.Floor[^] method.

Example:
C#
Math.Round(4.4); //Returns 4.0.
Math.Round(4.5); //Returns 4.0.
Math.Round(4.6); //Returns 5.0.


You can do it in this way to:
C#
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;  //Result: 1234

More about Casting and Type Conversions[^].
Please, read it: Whats the main difference between int parse and convert to int32[^]
 
Share this answer
 
v2
You need to use
C#
Math.Ceiling(val);
 
Share this answer
 
Comments
BillW33 31-May-12 9:13am    
Short and to the point; +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900