如何将浮点值转换为varchar值 [英] How do I convert a float value to a varchar value
本文介绍了如何将浮点值转换为varchar值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have value contains decimal (15,7) as datatype. It contains 5 digits after decimal point. My value is : 123.107157. When I try to convert the value into "varchar" then in the Output I get 123.107. I want result as 123.10705 after converting the value into varchar.
我尝试过:
What I have tried:
DECLARE @test1 decimal(15,7),
@test2 nvarchar(10)
SELECT @test1 = 123.1071570
- 案例1:
--Case 1 :
SELECT cast(@test1 as float) AS value1
-- Ans : 123.107157 -- Value which I got after using cast()
SELECT convert(varchar,cast(@test1 as float)) AS value2
-- Ans : 123.107 Value which I got after converting it to varchar
- 案例2:
-- Case 2 :
SELECT convert(DOUBLE PRECISION, @test1) AS value1
-- Ans : 123.107157 -- Value which I got after using DOUBLE
SELECT convert(varchar,convert(DOUBLE PRECISION, @test1)) AS value2
-- Ans : 123.107 Value which I got after converting it to varchar
推荐答案
问题是123.1071570不应显示为123.10715到五个位置 - 它应显示为123.10716,因为它会被舍入。
这很简单:
The problem is that 123.1071570 shouldn't display as 123.10715 to five places - it should display as 123.10716 because it gets rounded.
That's trivial to do:
SELECT CONVERT(VARCHAR, CAST(@test1 AS DECIMAL(15,5))) AS LongDecimal
这篇关于如何将浮点值转换为varchar值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文