Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Rounding Up with given precision


Rounding Up with given precision

Author
Message
phoenix_
phoenix_
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 31
I would like to round up some figures with given precision. So far I can't find a way to do it. ROUND function doesn't work for me because it can't round up, CEILING doesn't work as well because the results is integer.
What I would like to achieve is a rounding up with give precision of 2 places after dot i.e.
5.0000016 should be 5.01
6.1000138 should be 6.11
7.1200073 should be 7.13

Is it any way to do it in SQL?
Adi Cohn-120898
Adi Cohn-120898
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2224 Visits: 6079
Maybe there is a better way, but here is one way:


declare @Num numeric(8,7)
set @Num = 0.0000001

SET @Num = CASE WHEN CAST(@Num AS NUMERIC(3,2)) = @Num THEN @Num ELSE CAST(@Num AS NUMERIC(3,2)) + 0.01 END
SELECT @Num




Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
phoenix_
phoenix_
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 31
It works for me. Thank you very much.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45147 Visits: 39923
Haven't proofed (I normally don't do rounding) it but why wouldn't ROUND(somenumber+.00499999,2,1) work for you?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Adi Cohn-120898
Adi Cohn-120898
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2224 Visits: 6079
Jeff Moden (5/6/2013)
Haven't proofed (I normally don't do rounding) it but why wouldn't ROUND(somenumber+.00499999,2,1) work for you?


This would work as long as it is guaranteed that that each number will have a digit greater then zero in one of the 2 most left digits after the period (Hope that I wrote it correctly in English:-)). Here is an example:


--with both numbers the user wanted to get 0.01
select ROUND(0.0100000+.00499999,2,1)
select ROUND(0.0000001+.00499999,2,1)




Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
Here is another option (The CTE is just how I passed in the sample data):



with TestData as (
select datavalue
from (
values
(5.0000016), -- should be 5.01
(6.1000138), -- should be 6.11
(7.1200073), -- should be 7.13
(5.0000000), -- should be 5.00
(6.1000000), -- should be 6.10
(7.1200000) -- should be 7.12
)dt(datavalue)
)
select
datavalue,
(ceiling(datavalue * 100) * 1.0) / 100
from
TestData;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
wolfkillj
wolfkillj
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 Visits: 2582
I came up with another solution based on the same mathematical concept as Lynn's, but I think Lynn's is more elegant:
with TestData as (
select datavalue
from (
values
(5.0000016), -- should be 5.01
(6.1000138), -- should be 6.11
(7.1200073), -- should be 7.13
(5.0000000), -- should be 5.00
(6.1000000), -- should be 6.10
(7.1200000) -- should be 7.12
)dt(datavalue)
)
select
datavalue,
(ceiling(datavalue * 100) * 1.0) / 100, -- Lynn's idea
CASE WHEN (datavalue * 100) % 1 > 0 THEN ROUND(datavalue, 2, 1) + 0.01 ELSE ROUND(datavalue, 2, 1) END -- My idea
from
TestData;



Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3254 Visits: 11771
Here is a solution using the ROUND function:
with test_data_cte as
(
select datavalue
from
(
values
(5.0000016), -- should be 5.01
(6.1000138), -- should be 6.11
(7.1200073), -- should be 7.13
(5.0000000), -- should be 5.00
(6.1000000), -- should be 6.10
(7.1200000) -- should be 7.12
) dt (datavalue)
)
select
datavalue,
rounded_datavalue =
convert(numeric(8,2),round(datavalue+0.0049999,2))
from
test_data_cte



Results:
datavalue                               rounded_datavalue
--------------------------------------- -----------
5.0000016 5.01
6.1000138 6.11
7.1200073 7.13
5.0000000 5.00
6.1000000 6.10
7.1200000 7.12

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search