Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
I want to round the decimal value like this
if 5.50 => 5
if 5.51 => 6 ..
I want if the decimal value after point getter than .50 then the value increment by 1.
I used Math.Round() function, but it's round after .49 decimal value..
Posted 21-Feb-13 1:31am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use a custom rounding system:
 
public int Round(double value)
{
    double decimalpoints = Math.Abs(value - Math.Floor(value));
    if (decimalpoints > 0.5)
        return (int)Math.Round(value);
    else
        return (int)Math.Floor(value);
}
 
Hope this helps. Smile | :)
  Permalink  
Comments
Jayanta Chatterjee at 21-Feb-13 6:50am
   
Thanks a lotttttttttttt Sir..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

public int myRound(double val)
{
    return (int)Math.Floor(val + .49);
}
I don't like "magic numbers" so you might want to define a const double and swap it for the 0.49
  Permalink  
v2
Comments
Jayanta Chatterjee at 21-Feb-13 6:55am
   
I think This is more efficient then previous answers...
Thanks Sir....
Thanks a Lottttttttttt...:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

basic and simple way , you can convert decimal (5.50) to int (5) and you can check for 5.50-5 greater or lower than .50 if bigger then apply your algorithim..but i dont know any other function does that automaticly.
  Permalink  
Comments
Jayanta Chatterjee at 21-Feb-13 6:44am
   
how to get after point(.) values?
boogac at 21-Feb-13 6:47am
   
decimal yourDecimal=5.50;
int temp = (int)yourDecimal;//or Convert.toInt32(..) 5.50 will be 5
decimal pointAfterDecimal= yourDecimal-(decimal)temp;
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try,
String.Format("{0: 0}", _decimal_value);
 
hopes this work for you.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 190
1 PIEBALDconsult 150
2 BillWoodruff 148
3 DamithSL 140
4 Jochen Arndt 125
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100