SQL中的小数位 [英] Decimal places in SQL

查看:168
本文介绍了SQL中的小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算百分比.一个例子是降至38589/38400

I am calculating percentages. One example is coming down to 38589/38400

因此百分比为100 *(38589/38400),等于100.4921875,但结果显示为100.

So the percentage is 100*(38589/38400) which equals something like 100.4921875, but the result shows up as 100.

如何使它显示为x小数位数?

How can I get it to be displayed with x number of decimals?

类似地,如果我希望将2显示为2.000000,是否可以同样地工作?

Similarly, will the same work if i'd like 2 to be displayed as 2.000000?

谢谢!

推荐答案

您可以将其转换为特定的数据类型,这样既可以保留数据类型,也可以四舍五入到一定的精度

You can cast it to a specific data type, which preserves the data type as well as rounding to a certain precision

select cast(100*(38589/38400) as decimal(10,4))

仅供参考

select 100*(38589/38400)
# returns 100.4922, not 100 for me

select cast(2 as decimal(20,6))
# output : 2.000000

这篇关于SQL中的小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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