HTJE在表中的字段类型为float(53)
试了下str, cast和convert,发现对于小数或大数,多少都存在一些问题,最后经过尝试终于找到一种满意的答案:
select cast(HTJE as decimal(20,2)) from T_HTGL where ID = 1002993
对于金额部分,这里设置了小数最多2位,但是尾部的0不会自动去除,怎么办?
经过很多测试,发现在SQL层面,始终搞不定。没办法,最后在C#语言层面,先将double转换为不带尾部0的字符串,再使用这个字符串。下面是一个测试例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Con1
{class Program{static void Main(string[] args){double[] list = { 0.10,0.1023,0.1234,1,12,123.12,123.10,12345678910,12345678910.12,12345678910.10,12345678910.1048,12345678910.1058,};for (var i = 0; i < list.Length; i++){var s1 = string.Format("{0:0.##}", list[i]);var s2 = list[i].ToString("0.##");Console.WriteLine("s1 = {0}, s2 = {1}", s1, s2);}}}
}
运行结果如下:
s1 = 0.1, s2 = 0.1
s1 = 0.1, s2 = 0.1
s1 = 0.12, s2 = 0.12
s1 = 1, s2 = 1
s1 = 12, s2 = 12
s1 = 123.12, s2 = 123.12
s1 = 123.1, s2 = 123.1
s1 = 12345678910, s2 = 12345678910
s1 = 12345678910.12, s2 = 12345678910.12
s1 = 12345678910.1, s2 = 12345678910.1
s1 = 12345678910.1, s2 = 12345678910.1
s1 = 12345678910.11, s2 = 12345678910.11
请按任意键继续. . .
可见,这个输出的字符串满足了要求,无尾部多余的0,且能实现保留2位小数并四舍五入!