函数round和trunc

1、round函数。

round函数能够按照数学规则进行四舍五入的进位,以保留小数点后要求的位数。

使用方法为

round(<小数>,<保留的位数>)

下面是两个例子:

select round(1.23456,3) from dual;


select round(1.23456,2) from dual;

select round(12.26,-1) from dual;

 2、trunc函数

trunc函数与round的功能相同,也能保留小数点后要去的位数,但是trunc不会按照数学规则仅为,只是单纯的截断

使用方法为:trunc(<小数>,<保留的位数>)

下面是几个例子:

select trunc(1.23456,3) from dual;

 select trunc(1.23456,4) from dual;

 select trunc(123,-1) from dual;

trunc除了可以阶段数字外,还可以截断日期:

select trunc(sysdate) from dual          返回当天的日期 精确到天
select trunc(sysdate, 'mm')   from   dual       返回当月第一天.
select trunc(sysdate,'dd') from dual         返回当前年月日
select trunc(sysdate,'yyyy') from dual         返回当年第一天
select trunc(sysdate,'d') from dual           返回当前星期的第一天
select trunc(sysdate, 'hh') from dual        返回当前小时的0分钟 
select trunc(sysdate, 'mi') from dual        返回当前分钟的0秒

SYS @nathondb> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

  SYS @nathondb> select sysdate from dual;

  SYSDATE
  -------------------
  2019-01-22 23:24:31


SYS @nathondb> select trunc(sysdate,'yyyy') from dual;

TRUNC(SYSDATE,'YYYY)
-------------------
2019-01-01 00:00:00

SYS @nathondb> select trunc(sysdate,'mm') from dual;

TRUNC(SYSDATE,'MM')
-------------------
2019-01-01 00:00:00

SYS @nathondb> select trunc(sysdate,'dd') from dual;

TRUNC(SYSDATE,'DD')
-------------------
2019-01-22 00:00:00

SYS @nathondb> select trunc(sysdate,'hh') from dual;

TRUNC(SYSDATE,'HH')
-------------------
2019-01-22 23:00:00

SYS @nathondb> select trunc(sysdate,'mi') from dual;

TRUNC(SYSDATE,'MI')
-------------------
2019-01-22 23:21:00

猜你喜欢

转载自www.cnblogs.com/nathon-wang/p/10306724.html