예제 #1
0
        private static string FormatMoney(
            decimal amount,
            string currency               = null,
            bool useCurrencySymbol        = true,
            DecimalDisplay decimalDisplay = DecimalDisplay.AlwaysShow)
        {
            if (currency == null)
            {
                return(GetDefaultFormattedString(amount));
            }

            IFormatProvider formatProvider = CurrencyHelper.GetCurrencyCulture(currency);

            if (formatProvider == null)
            {
                return(GetDefaultFormattedString(amount));
            }

            if (!useCurrencySymbol)
            {
                var numberFormatInfo = (NumberFormatInfo)((CultureInfo)formatProvider).NumberFormat.Clone();
                numberFormatInfo.CurrencySymbol = string.Empty;
                formatProvider = numberFormatInfo;
            }

            var amountHasDecimals = amount % 1 != 0;

            var format =
                decimalDisplay == DecimalDisplay.AlwaysShow ||
                (decimalDisplay == DecimalDisplay.ShowNonZeroOnly && amountHasDecimals)
                    ? "C"
                    : "C0";

            return(amount.ToString(format, formatProvider).Trim());
        }
예제 #2
0
 public static string Format(this decimal amount, string currency, bool useCurrencySymbol, DecimalDisplay decimalDisplay)
 {
     return(FormatMoney(amount, currency, useCurrencySymbol, decimalDisplay));
 }