【问题标题】:Get Exact format string with respect to given format specifier获取关于给定格式说明符的确切格式字符串
【发布时间】:2016-09-22 20:32:24
【问题描述】:

.net 中有很多格式说明符,例如C代表货币,D代表十进制。 许多定义的格式可以是here

如何根据给定的语言环境获得格式说明符后面的确切格式。

例如(我假设这里是 en-US 语言环境)

GetFormatSpecifierText("C") 应该返回我 "$" #,##0.00

GetFormatSpecifierText("F") 应该返回我 "#,##0.00

【问题讨论】:

  • 你可以使用custom number format
  • 实际上我需要将欲望格式传递给excel。所以如果我得到 "$" #,##0.00,那么我可以很容易地传递到 excel 中,它会被转换为货币。但是如果我传递一些值(例如 $ 100.00),那么它将被视为字符串(这不是我的要求)。此外,我不知道自定义格式是什么,所以我想在运行时提取。
  • 我发现了类似 NumberFormatinfo.CurrencyPositivePattern 的内容。但它给了我一些'int'值。寻求获得返回字符串模式的东西。

标签: c# .net c#-4.0 c#-3.0


【解决方案1】:

我编写下面的代码来获取货币格式模式。

在这里,将参数 numberFormat 传递为 [Currency]#,##0.00fr-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

当我坚持使用特定的 Currency 格式的 excel 时,我编写了特定于货币的代码。其他格式很容易被 excel 处理(同样由 Murray Foxcroft 在回答的评论中评论)。

【讨论】:

    【解决方案2】:

    看看 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"));
       }
    }
    

    【讨论】:

    • 实际上我需要将欲望格式传递给excel。所以如果我得到 "$" #,##0.00,那么我可以很容易地传递到 excel 中,它会被转换为货币。但是如果我传递一些值(例如 $ 100.00),那么它将被视为字符串(这不是我的要求)。
    • Excel 也具有文化意识。你是如何“将它传递给 Excel”的 - 你是否使用自动化 - 如果是这样,请参见此处:stackoverflow.com/questions/1362315/…
    • 正确的 Murray,但如果是货币,它会保持不变。如果我通过fr-FR进行货币转换并在en-US中编写代码,我们如何将精确的货币格式字符串(必须是货币格式并且可以进行算术运算)写入excel?这就是我要求格式模式字符串的原因。顺便说一句,我用一些棘手的解决方案为我实施了货币。感谢您的回答和cmets。
    • @NeoAsh 如果您找不到更简洁的方法,那么您可以从文化的 NumberFormatInfo 计算格式字符串: NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat; Console.WriteLine(nfi.);
    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2011-06-19
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多