I need to format a decimal to a minor currency eg 10.00 should be 1000.
decimal currency = 10.00m;
System.Console.WriteLine(currency.ToString("######"));
Produces 10, how do I get the decimal points to be added to that?
解决方案非常简单
* 100
I would create an extension method like this that would produce always the expected result, with the required number of decimals:
public static class DecimalExtension
{
public static string FormatAsMinorCurrency(this decimal value) {
var numberFormat = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
numberFormat.CurrencyDecimalDigits = 2;
numberFormat.CurrencyDecimalSeparator = ".";
numberFormat.CurrencySymbol = "";
numberFormat.CurrencyGroupSeparator = "";
return value.ToString("c", numberFormat).Replace(".", "");
}
}
The results:
1.FormatAsMinorCurrency()
100
10.FormatAsMinorCurrency()
1000
1000000.34102350915091M.FormatAsMinorCurrency()
100000034
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.