DecimalExtension.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. namespace GreenTree.Strohrmann.ERP.Core.Extension
  6. {
  7. public static class DecimalExtension
  8. {
  9. /// <summary>
  10. /// Trims trailing zeros of decimal
  11. /// </summary>
  12. /// <param name="value">The value.</param>
  13. /// <returns>The same number without trailing zero decimal places.</returns>
  14. public static decimal TrimEnd(this decimal value)
  15. {
  16. var decimalSplit = value.ToString(CultureInfo.InvariantCulture.NumberFormat).Split('.');
  17. if (decimalSplit.Length == 1)
  18. return value;
  19. var onlyZero = true;
  20. for (int i = 0; i < decimalSplit[1].Length; i++)
  21. {
  22. if (decimalSplit[1][i] != '0')
  23. {
  24. onlyZero = false;
  25. break;
  26. }
  27. }
  28. return onlyZero ? Convert.ToDecimal(decimalSplit[0]) : value;
  29. }
  30. }
  31. }