获取浮点数小数点后的位数(带或不带小数部分) [英] Get the number of digits after the decimal point of a float (with or without decimal part)

查看:36
本文介绍了获取浮点数小数点后的位数(带或不带小数部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格中有以下 Amount (float) 列表.

I have following list of Amount (float) in my table.

Amount
123
123.1
123.0123
123.789456

我怎样才能得到小数点后的位数.

How can i get the number of digits after the decimal point.

重复?:我已经检查了现有的帖子,但没有正确的方法来处理 float 数字 带或不带小数部分.

Duplicate ?: I have checked already existing posts, but there is no correct way to handle the float numbers with or without decimal part.

结果

Amount      Result
123         0
123.1       1
123.0123    4
123.789456  6

编辑:在花了一些宝贵的时间之后,我找到了一些相对简单的脚本来处理这个问题.我的答案是 下面

EDIT : After spending some valuable time, i have found some relatively simple script to handle this. My answer is below

推荐答案

我找到了一些简单的脚本(相对于我而言)来处理这个问题.

I found some simple script (relatively to me) to handle this.

ISNULL(NULLIF(CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))),0) - 1,0)

这里的 ISNULL(NULLIF 只是处理没有小数部分的浮点数.如果没有没有小数部分的值,那就很简单了

Here the ISNULL(NULLIF is only to handle the float without decimal part. If there is no values without decimal part, then it is very simple

CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))) -1 

希望对你有帮助.完整脚本如下

Hope this will be helpful to you. Full script below

declare @YourTable table (Amount float)
insert into @YourTable
values(123),(123.1),(123.0123),(123.789456)

SELECT  ISNULL(NULLIF(CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))),0) - 1,0)
FROM    @YourTable

SELECT  CHARINDEX('.',REVERSE(CONVERT(VARCHAR(50), Amount, 128))) -1 
FROM    @YourTable

这篇关于获取浮点数小数点后的位数(带或不带小数部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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