去除小数雪花后的尾随零 [英] Removing trailing zeroes after decimal Snowflake

查看:12
本文介绍了去除小数雪花后的尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从小数点后的数值列中删除尾随零。例如:

0.978219150000 -> 0.97821915
0.650502591918 -> 0.650502591918
0.975479450000 -> 0.97547945

数据类型为数字(38,12)。有没有办法去掉我上面提到的尾随零?

推荐答案

您可以尝试强制转换为浮点:

create or replace table test (a NUMBER(38,12));
insert into test values (0.97821915), (0.650502591918), (0.975479450000);

select a from test;
+----------------+
|              A |
|----------------|
| 0.978219150000 |
| 0.650502591918 |
| 0.975479450000 |
+----------------+

select a::float from test;
+--------------+
|     A::FLOAT |
|--------------|
| 0.97821915   |
| 0.6505025919 |
| 0.97547945   |
+--------------+

但是,根据您要实现的目标,由于潜在的舍入问题,使用浮点数可能不是一个好主意。

更新:

我尝试了regexp版本,不确定是否遗漏了任何测试用例:

create or replace table test (a NUMBER(38,12));
insert into test values 
    (0.97), 
    (0.650502591918), 
    (0.975479450000), 
    (10000), 
    (1450000), 
    (12.2000), 
    (14.0200);

select regexp_replace(
    a::varchar, 
    '^([0-9]+)$|' ||
    '^([0-9]+).0*$|' ||
    '^([0-9]+.[0-9]{1,}[1-9])0*$|' ||
    '^([0-9]+.[1-9])0*$', '\1\2\3\4'
) as a from test;

+----------------+
| A              |
|----------------|
| 0.97           |
| 0.650502591918 |
| 0.97547945     |
| 10000          |
| 1450000        |
| 12.2           |
| 14.02          |
+----------------+

其中:

^([0-9]+)$                   -> will cover the integer like 10000
^([0-9]+).0*$               -> will cover integer like 10.000000
^([0-9]+.[0-9]{1,}[1-9])0*$ -> will cover 14.0200000
^([0-9]+.[1-9])0*$.         -> will cover 12.20000 or 0.97540000

这篇关于去除小数雪花后的尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆