10
Answers

how to round of in sql

siva nathan

siva nathan

7y
731
1
I have a table column values like below
Hours
1.3000
0.4500
3.4500
2.4000
my expected result is
Hours
2
1
4
3
how to achieve this output without using ceiling because ceiling convert 1.10  as 2 .my case is whenever the value is like  .30 or 1.30 means convert 1 and 2 how to get this
Answers (10)
3
Rafnas T P

Rafnas T P

139 13.4k 4.5m 7y
for your reference,
 
create function as below
 
  1. private static string RoundOf(string _Value)    
  2.        {    
  3.            string[] _Valuesplit = Convert.ToDouble(_Value).ToString("F2").Split('.');    
  4.            int _intValue2 = Convert.ToInt16(_Valuesplit[1][0].ToString());    
  5.            if (_intValue2 >= 3)    
  6.            {    
  7.                _Value = Math.Ceiling(Convert.ToDouble(_Value)).ToString();    
  8.            }    
  9.            else    
  10.            {    
  11.                _Value = Math.Floor(Convert.ToDouble(_Value)).ToString();    
  12.            }    
  13.     
  14.            return _Value;    
  15.        }   
 
and pass your value as string to this function
for example,
 
  1. string _Value = "2.30";      
  2.   _Value = RoundOf(_Value);     //ex1    
  3.     
  4. string _Value = "2.15";      
  5.   _Value = RoundOf(_Value);  //ex2   
 try this, I have already tried this is working fine
2
Nitin Sontakke

Nitin Sontakke

133 13.6k 15.2k 7y
@Amit Kumar, Vivek Kumar and Amit Gupta,
 
Siva aiming to use round function which is written for decimal system to round of hours and minutes. This is not possible. In hours and minutes 60 = 100 and 30 = 50. How language functions written for decimal system are expected to know that?
 
In some of his previous posts, I tried to explain to him that these are TWO ENTIRELY DIFFERENT things and there is no native support from language.
2
Rafnas T P

Rafnas T P

139 13.4k 4.5m 7y
hi siva,
for this I have already give you a solution have tried that.
 
2
Amit Gupta

Amit Gupta

NA 22.9k 247.1k 7y
Your question was not clear to me. In the first para, you are expecting 1.3000 to 2, 0.4500 to 1. This can be achieved by using ceiling function.
 
And on the last para, you are asking  .30 or 1.30 to 1 and 2. What was that !!
 
Can you brief the program scenario, for what purpose it was going to be put this logic. 
2
Vivek Kumar

Vivek Kumar

157 11.8k 5.7m 7y
Hi Siva,
You can get all value using ceiling function whatever you provide in your expected result. Can you elaborate more? 
1
Amit Kumar

Amit Kumar

275 6.6k 3.9m 7y
Hi Nitin Sontakke,
                              I  am agree with you.
 

 

 
1
Amit Kumar

Amit Kumar

275 6.6k 3.9m 7y
Hi Dear,
Use simple sql function celling then you will get exact result.
 
SELECT CEILING(1.30);
SELECT CEILING(0.4500);
SELECT CEILING(3.4600);
SELECT CEILING(2.4000);
 
 
1
Rafnas T P

Rafnas T P

139 13.4k 4.5m 7y
hi siva, have you checked my solution
1
Rafnas T P

Rafnas T P

139 13.4k 4.5m 7y
I have already given for your refernce see below
1
siva nathan

siva nathan

770 836 245.8k 7y
 Hi Rafnas,
can you pls tell me the answer again ,im forgot to use