金额保留两位小数

 

进行四舍五入:

Convert.ToDecimal(1.66666.ToString("f" + decimalCount))

不进行四舍五入:

public static decimal CutDecimalWithN(decimal d, int n)
{
string strDecimal = d.ToString();
int index = strDecimal.IndexOf(".");
if (index == -1 || strDecimal.Length < index + n + 1)
{
strDecimal = string.Format("{0:F" + n + "}", d);
}
else
{
int length = index;
if (n != 0)
{
length = index + n + 1;
}
strDecimal = strDecimal.Substring(0, length);
}
return Decimal.Parse(strDecimal);
}

猜你喜欢

转载自www.cnblogs.com/nayilvyangguang/p/12572052.html