简体   繁体   English

从十进制中删除一些尾随零

[英]Remove some of trailing zeros from decimal

I have column decimal(10,5)VALUE , and column intPICTURE .我有列decimal(10,5)VALUE和列intPICTURE

So, I need to round VALUE to PICTURE digits after decimal point.所以,我需要将VALUE舍入到小数点后的PICTURE数字。

For example:例如:

  • if VALUE = 10.50000 and PICTURE = 2 , I want to get 10.50 ;如果VALUE = 10.50000PICTURE = 2 ,我想得到10.50
  • if VALUE = 0.15371 and PICTURE = 3 , I want to get 0.154如果VALUE = 0.15371PICTURE = 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM