The amount of two decimal places

 

Rounding:

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

Not be rounded up:

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);
}

Guess you like

Origin www.cnblogs.com/nayilvyangguang/p/12572052.html