using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace GreenTree.Strohrmann.ERP.Core.Extension
{
    public static class DecimalExtension
    {
        /// <summary>
        /// Trims trailing zeros of decimal
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The same number without trailing zero decimal places.</returns>
        public static decimal TrimEnd(this decimal value)
        {
            var decimalSplit = value.ToString(CultureInfo.InvariantCulture.NumberFormat).Split('.');

            if (decimalSplit.Length == 1)
                return value;

            var onlyZero = true;

            for (int i = 0; i < decimalSplit[1].Length; i++)
            {
                if (decimalSplit[1][i] != '0')
                {
                    onlyZero = false;
                    break;
                }
            }

            return onlyZero ? Convert.ToDecimal(decimalSplit[0]) : value;
        }
    }
}
