.net中有很多格式说明符,例如C表示货币,D表示小数。 许多定义的格式可以是here。
如何根据给定的区域设置获得格式说明符后面的确切格式。
E.g。 (我在这里假设en-US语言环境)
GetFormatSpecifierText(" C")应该返回" $" #,## 0.00
GetFormatSpecifierText(" F")应该返回"#,## 0.00
答案 0 :(得分:0)
了解Culture如何与格式说明符结合使用。我相信您可能正在寻找以下code from MSDN:
中的覆盖public class userOverrideSample
{
[WebMethod]
public String UserLocalSetting()
{
int i = 100;
// Sets the CurrentCulture to French in France.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
// Displays i formatted as currency for the CurrentCulture.
// Due to operating system differences, you cannot be sure what currency
// symbol will be used.
return (i.ToString("c"));
}
[WebMethod]
public String OverrideUserSetting()
{
int i = 100;
// Sets the CurrentCulture to French in France.
// Uses the CultureInfo constructor that takes a
// useUserOverride parameter.
// Sets the useUserOverride value to false.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR", _
false);
// Displays i formatted as currency for the CurrentCulture.
// This will override any user settings and display the euro symbol.
return (i.ToString("c"));
}
}
答案 1 :(得分:0)
我在下面编写代码来获取货币格式模式。
此处,将param numberFormat作为 [Currency]#,## 0.00 和 fr-FR 区域设置传递;并且 ### 0,00“€”作为愿望和预期的o / p。
static string[] CurrencyPositivePattern = { "$n", "n$", "$ n", "n $" };
static string[] CurrencyNegativePattern = { "($n)", "-$n", "$-n", "$n-", "(n$)",
"-n$", "n-$", "n$-", "-n $", "-$ n",
"n $-", "$ n-", "$ -n", "n- $", "($ n)",
"(n $)" };
internal static string GetCurrencyPattern(System.Globalization.NumberFormatInfo numberFormatInfo, string numberFormat)
{
numberFormat = numberFormat.Replace("[Currency]", string.Empty);
int pos = numberFormatInfo.CurrencyPositivePattern;
int neg = numberFormatInfo.CurrencyNegativePattern;
string currencySymbol = string.Format("\"{0}\"", numberFormatInfo.CurrencySymbol);
string excelPattern = string.Concat(CurrencyPositivePattern[pos].Replace("n", numberFormat).Replace("$", currencySymbol),
";",
CurrencyNegativePattern[neg].Replace("n", numberFormat).Replace("$", currencySymbol));
return excelPattern;
}
我用于实施的参考文献很少: https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencynegativepattern(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencypositivepattern(v=vs.110).aspx
当我坚持使用特定的货币格式的excel时,我会编写特定于货币的代码。其他格式很容易通过excel处理(同样也由 Murray Foxcroft 在评论的评论中评论)。