在C#中通过toString()可以将double类型变量去除小数点后的0或者加上千分符,但是我两种都需要请问该如何实现?
------解决思路----------------------
double m = 1234222.010;
string s1 = FormatValue(Convert.ToDecimal(m.ToString("0")));
网上的 方法:
private string FormatValue(decimal value)
{
StringBuilder sb = new StringBuilder();
string sValue = value.ToString();
List<string> list = new List<string>();
string temp = sValue;
bool includeFloat = sValue.LastIndexOf(".") != -1;
if (includeFloat)
{
//temp = temp.Substring(0, sP);
temp = ((int)value).ToString();
}
int templength = temp.Length;
if (temp.Length > 3)
{
while (templength > 3)
{
list.Add(temp.Substring(templength - 3, 3));
templength -= 3;
}
//最前面的添加进来
list.Add(temp.Substring(0, temp.Length - list.Count * 3));
for (int i = list.Count - 1; i > 0; i--)
{
sb.Append(list[i] + ",");
}
sb.Append(list[0]);
if (includeFloat)
{
sb.Append(sValue.Substring(sValue.LastIndexOf(".")));
}
}
else
{
return sValue;
}
return sb.ToString();
}
------解决思路----------------------
double value = 16325.62;
var s = value.ToString("#,#.##");