[英]Remove some of trailing zeros from decimal
I have column decimal(10,5)
— VALUE , and column int
— PICTURE .我有列
decimal(10,5)
— VALUE和列int
— PICTURE 。
So, I need to round VALUE to PICTURE digits after decimal point.所以,我需要将VALUE舍入到小数点后的PICTURE数字。
For example:例如:
VALUE = 10.50000
and PICTURE = 2
, I want to get 10.50
;VALUE = 10.50000
和PICTURE = 2
,我想得到10.50
;VALUE = 0.15371
and PICTURE = 3
, I want to get 0.154
VALUE = 0.15371
和PICTURE = 3
,我想得到0.154
If I use just Round()
or Cast(Round(...) as nvarchar)
, then I have trailing zeros.如果我只使用
Round()
或Cast(Round(...) as nvarchar)
,那么我有尾随零。 If I Cast()
to float
, then I loose zeros.如果我
Cast()
到float
,那么我会丢失零。
Is there any solution?有什么解决办法吗?
You can use the str()
function with trim()
:您可以将
str()
函数与trim()
:
select trim(str(value, 20, picture))
str()
converts a number to a string with the specified length and precision. str()
将数字转换为具有指定长度和精度的字符串。 Sadly, it produces a fixed length string, left padded with spaces.可悲的是,它产生了一个固定长度的字符串,左边用空格填充。 The
trim()
removes the spaces. trim()
删除空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.