Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 将数字舍入到10的倍数_Postgresql_Postgresql 9.1 - Fatal编程技术网

Postgresql 将数字舍入到10的倍数

Postgresql 将数字舍入到10的倍数,postgresql,postgresql-9.1,Postgresql,Postgresql 9.1,在PostgreSQL中,如何轻松地将数字舍入到10的倍数 例如: In Out 100 --> 100 111 --> 120 123 --> 130 样本数据: create table sample(mynumber numeric); insert into sample values (100); insert into sample values (111); insert into sample values (123); 我可以使用: selec

在PostgreSQL中,如何轻松地将数字舍入到10的倍数

例如:

In      Out
100 --> 100
111 --> 120
123 --> 130
样本数据:

create table sample(mynumber numeric);

insert into sample values (100);
insert into sample values (111);
insert into sample values (123);
我可以使用:

select 
 mynumber,
 case
    when mynumber = round(mynumber,-1) then mynumber 
    else round(mynumber,-1) + 10 end as result
from
 sample;
这很管用,但看起来很难看。有没有更简单的方法


您可以发现SQLFiddle

(number+9)/10可以直接应用于所有情况,以获得10的整数倍。不知道如何在postgresql中使用。数字可以是负数吗?如果是,请指定“向上舍入”。有小数吗?在我的例子中,我只有正数。有小数位数,很好。我使用带小数点的除数,而不是显式转换为数字,例如
ceil(a/10.0)*10
select ceil(a::numeric / 10) * 10
from (values (100), (111), (123)) s(a);
 ?column? 
----------
      100
      120
      130