[英]Round to 0.5 in sql
I have number data: 我有数字数据:
123.42
12.54
1.02
2.99
For each one I want to obtain the closest number to a 0.5 step. 对于每一个,我想获得最接近0.5的数字。 So
所以
func(123.42)=123.0
func(12.54)=12.5
func(1.02)=1.0
func(2.99)=2.5
Any clues? 有什么线索吗? I'm trying with a
trunc((x-floor(x))*5)/5)
but can't get anything. 我正在尝试使用
trunc((x-floor(x))*5)/5)
但是什么也收不到。
val := round(val*2) / 2;
Such expresion should be what you described in question. 这样的表达应该是您所描述的。 So function is:
所以功能是:
create or replace function func(val number) return number
is
begin
return round(val*2) / 2;
end;
But looking on examples you don't want closest number but highest number smaller than your value rounded to 0.5. 但是,在示例中,您不希望有最接近的数字,而想要小于您的值的最大数字舍入为0.5。 And this you will obtain with:
而您将获得:
create or replace function func(val number) return number
is
begin
return floor(val*2) / 2;
end;
If you would like the smallest number greater than value rounded to 0.5 it would be: 如果您希望大于数值的最小数字四舍五入为0.5,则为:
create or replace function func(val number) return number
is
begin
return ceil(val*2) / 2;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.