Tools.cs 966 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace StageController
  7. {
  8. public class Tools
  9. {
  10. public static string CutDecimalWithNToString(decimal d, int n)
  11. {
  12. return CutDecimalWithN(d, n).ToString();
  13. }
  14. public static decimal CutDecimalWithN(decimal d, int n)
  15. {
  16. string strDecimal = d.ToString();
  17. int index = strDecimal.IndexOf(".");
  18. if (index == -1 || strDecimal.Length < index + n + 1)
  19. {
  20. strDecimal = string.Format("{0:F" + n + "}", d);
  21. }
  22. else
  23. {
  24. int length = index;
  25. if (n != 0)
  26. {
  27. length = index + n + 1;
  28. }
  29. strDecimal = strDecimal.Substring(0, length);
  30. }
  31. return Decimal.Parse(strDecimal);
  32. }
  33. }
  34. }