using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StageController { public class Tools { public static string CutDecimalWithNToString(decimal d, int n) { return CutDecimalWithN(d, n).ToString(); } 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); } } }