sql server update double rounding from steps - sql

I have this issue: in my database there's a field that is a double value. I need that this value is rounded by a constant step:
for example I set a step of 20.0 and I wish that a value 17.8 become 20.0, a value of 31.2 become 40.0 etc..
how can I do this with a query?
thanks in advance

declare #stepsize float = 20.0
declare #val float = 17.6
select CEILING(#val / #stepsize) * #stepsize -- Result 20.0
set #val = 31.2
select CEILING(#val / #stepsize) * #stepsize -- Result 40.0
It's unclear from your question what 6.0 should 'round' to with a stepzie of 20.0. Should it round down to zero?

You do this by dividing the value by the step, rounding it and then multiplying by the step. Most SQL dialects have arithmetic functions.

Related

Pandas keep decimal part of values like .998344 after round(2) applied to series

I have dataset with float values with 6 decimals. I need to round it to two decimals.The problem becams with some floats nearly close to 1. After applying round(2) I got 1.00 instead of 0.99. May be this is mathematically right, but I need to have values like 0.99. My customer needs two decimals as result, I cant change it.
ones_list = [0.998344, 0.996176, 0.998344, 0.998082]
df = pd.DataFrame(ones_list, columns=['value_to_round'])
df['value_to_round'].round(2)
1.0
1.0
1.0
1.0
I see a few options:
use floor instead of round (but would you have the same issue with 0.005?)
use clip to set a maximum (and a min?) value in the column of 0.99:
df['value_to_round'].round(2).clip(upper=0.99)
Please refer to the basic of rounding in math, you are trying to round 2 digits behind the dot using .round(2)
if you round 0.999 using .round(2), of course you'll get 1.0 because the last '9' digit (0.009) will become 0.01 thus will be added to 0.09 and become 0.1 added again with 0.9 and finally becomes 1.0
If you really want to have values like 0.99, just take the two decimals behind the dot. You can try either the following methods:
import math
df['value_to_round'] = df['value_to_round'] * 100
df['value_to_round'] = df['value_to_round'].apply(math.trunc)
df['value_to_round'] = df['value_to_round'] / 100
or
df['value_to_round'] = df['value_to_round'].astype(str)
df['value_to_round'] = df['value_to_round'].str[:4]
df['value_to_round'] = df['value_to_round'].astype(float)
I experienced the same thing when I was trying to show R squared value, what I did is just use .round(3), because 3 digits decimal wouldn't hurt
I hope this helps :)
df['value_to_round'] = [x if x < 1 else 0.99 for x in df['value_to_round'].round(2)]

SQL round percent up to nearest .1

I have a columns of percentages and i want them to always round up to the nearest .1
for example
.005 --> .1
.11 --> .2
.3256 -->.4
.6797 -->.7
I am trying ceiling and floor, but they give me all whole numbers
I have tried this, but it still rounds as it normally would work
round(n * 10, 0) / 10 as [nearest 0.10]
You can always use ceiling() and arithmetic:
select ceiling(percentage * 10) / 10.0
round() function can be the easiest option available.
select round(1.02345443,2) from dual
where the 1st argument is the input number and the second digit is digits after decimal point.
Please refer to this official doc for more reference.
Another option, with modular arithmetic:
DECLARE #a DECIMAL(5,5) = 0.000005 --insert your number here
PRINT ROUND(#a, 1) + CASE WHEN #a % 0.1 < 0.05 AND #a % 0.1 > 0 THEN 0.1 ELSE 0 END
This tests to see if the remainder when divided by 0.1 is between 0 and 0.05 (meaning it rounded down), and adds 0.1 to the rounded value if so.

spliting a decimal values to lower decimal in sql server

How can i split a decimal value into two decimal values.
if the decimal number which has fractional part that is less than .50 or greater than .50 .it should split in such a way that first no should end with only .00 or .50. second value should contain the remaining factorial value.
ex. 19.97 should return 19.50 & 0.47
19.47 19.00 & 0.47
You can "floor" to the highest multiple of 0.5 by multiplying by 2, calling FLOOR, then dividing by 2. From there just subtract that from the original value to get the remainder.
DECLARE #test decimal(10,7)
SELECT #test =19.97
SELECT
FLOOR(#test * 2) / 2 AS base,
#test - FLOOR(#test * 2) / 2 AS fraction
or to reduce duplication
SELECT
base,
#test - base AS fraction
FROM ( SELECT FLOOR(#test * 2) / 2 AS base )
Declare #money money
Set #money = 19.97
Select convert(int,#money - (#money % 1)) as 'LeftPortion'
,convert(int, (#money % 1) * 100) as 'RightPortion'
Observe that doubling 0.5 gives 1, which is a whole number. This leads to a simple algorithm:
Double the number
Split it into a whole and a fractional parts by using floor(x) and x-floor(x)
Divide each part separately by 2 to give you the results that you need.
Let's take your numbers as an example:
19.97 * 2 = 39.94
Whole part = 39, fractional part = 0.94
Dividing each part by 2 individually, we get
39/2 = 19.50
0.94/2 = 0.47
19.47 * 2 = 38.94
Whole part = 38, fractional part = 0.94
Dividing each part by 2 individually, we get
38/2 = 19.00
0.94/2 = 0.47
You would need to look into the MODULO operator in SQL Server.
SELECT
19.97 as myDecVal ,
19.97 % 0.5 AS decGreaterThan50,/*should return 0.47*/
19.97- (19.97 % 0.5) as roundedToNearestZeroPoint5 /*should return 19.50*/;

Sql issue in calculating formulas

I have a problem when i'm trying to calculate in a view a formula whose result is smaller than 1.
e.g. I have the next formula: Arenda*TotalArea/10000 as TotalArenda
If I have Arenda=10 and TotalArea=10 I get TotalArenda=0,00 when normally should be 0.01
Thanks
Make Arenda = 10.0 and TotalArea = 10.0 instead of 10 and 10. This will force SQL not to use integer math and you will get your needed accuracy.
In fact, the only way I can get 0.0 as the result is if the Arenda is 10 (integer) while at least one of TotalArea or 10000 contain a decimal and a trailing 0, and only if I override order of operations by grouping using parentheses such as
select 10.0* (10/10000) as blah
If all are integers you get 0. If all contain decimals you get 0.01. If I remove the parentheses, I get 0.01 if ANY of them are non-integer types.
If precision is highly important I would recommend you cast to decimals and not floats:
select CONVERT(decimal(10,2), Arenda) * CONVERT(decimal(10,2), TotalArea) / 10000.0
You are using colunns, so changing the type may not be feasible. SQL Server does integer division on integers (other databases behave differently). Try one of these:
cast(Arenda as float)*cast(TotalArea as float)/10000
or:
Arenda*TotalArea/10000.0

Rounding down with a number. Finding the n in n.mp

Bit of an odd question. I'm using cocoa.
I have a series of numbers e.g.:
0.87
0.32
1.12
2.34
8.82
12.66
and I want to get the bit before the decimal place, with no rounding.
Like this:
0.87 -> 0
0.32 -> 0
1.12 -> 1
2.34 -> 2
8.82 -> 8
12.66 -> 12
I can round the numbers with no problem, but I can't figure out how to just take the 'rounded down' figure in an elegant and non complicated way. Can you help?
use floor(double). Or cast to an int
Simply cast it to an int and you're good to go.
NSLog(#"n is %i", n);
%i will automatically cast it to int. :P
float x = 0.89;
int y = x;
int round down automatically.