【问题标题】:How to get the format string for a specified currency and culture如何获取指定货币和文化的格式字符串
【发布时间】:2019-10-24 18:30:26
【问题描述】:

我有显示货币金额的 SSRS 报告。他们需要同时具备文化意识和货币意识。一些报告在同一张表中显示不同的货币。我对文化意识没有任何问题。麻烦的是货币格式。 重要的是,当我导出到 Excel 时,这些货币字段中的值必须可以按数字排序。这意味着单元格值必须是数字,因此我不能使用许多其他帖子最终使用的普通 .ToString("C",cu​​lture) 函数。我需要将数值保留在字段中并将 .NET 的格式字符串应用于数字(例如“'$'#,0.00;('$'#,0.00)”)。这样,Excel 会将值视为数字以进行排序,但会显示格式正确的货币。

是否可以使用代码修改 NumberFormatInfo 实例,然后以某种方式返回格式化程序的字符串值,例如“'€'#,0.00;('€'#,0.00)”?

var numberFormat = new CultureInfo("en-US").NumberFormat;
numberFormat.CurrencySymbol = "€";
return numberFormat.GetCurrencyFormatString(); //this is an imaginary function that I need to return "'€'#,0.00;('€'#,0.00)"

我已尝试根据每行的货币信息以编程方式设置货币符号。据我所知,SSRS 不允许我使用表达式来设置货币符号。它只提供一个下拉列表。

当我显示货币代码(例如 USD、CAD)时,我的用户不喜欢它,所以我坚持显示符号(例如 $、CA$)。

【问题讨论】:

  • 这里有一些提示。 stackoverflow.com/questions/6924067/…。如果要将其导出为 excel,则不需要格式化。 Excel 将更好地理解未格式化的数字,并将应用它自己的格式。
  • 感谢霍尔格的回复。但是,该帖子中的答案建议使用 String.Format() 函数,该函数将返回一个字符串。如果我在单元格中使用字符串,Excel 将作为字符串排序。另外,我的要求之一是在同一张表中显示不同的货币。 Excel 无法知道应用哪个货币符号,除非我告诉它使用哪个。这就是我需要格式字符串的原因——因为 Excel 可以理解它。
  • 你要求格式化,当然你会得到一个字符串,其他的不能格式化。推荐什么取决于您如何尝试将任何内容传输到 excel - 通过剪贴板;它很可能是一个文本。通过 Interop,您可以直接传递您的替身。使用 Copy&Paste Excel 将尽可能好地解析传入的文本,我认为它不会将多个符号识别为货币,也不会识别任何其他单位。作为后备,如果它无法解析,它仍然是一个字符串。因此,您对数字进行的格式越多,它就越有可能保留为字符串。
  • 我在这里专门讨论 SSRS。 SSRS 导出到 Excel 时,单元格中的值必须是数字。应用于数字的格式是我需要掌握的。如果我将 '$'#,0.00;('$'#,0.00) 格式应用于数字,Excel 仍然会知道该值是数字,这很好。它会将数字格式化为美元金额。我正在寻找一个函数,它给我类似 '$'#,0.00;('$'#,0.00) 的东西,它尊重正确的货币符号以及当前的文化设置,这会影响货币符号的显示位置(之前或在数字之后)
  • 只要你不澄清你所说的“出口”是什么意思,这个讨论是没有用的。您可以使用 Interop-Api,您可以使用剪贴板,您可以将剪贴板用作纯文本或 html 格式,您可以“另存为”xlsx 文件,而无需联系 excel。

标签: c# .net reporting-services currency-formatting


【解决方案1】:

据我所知,您需要使用 CultureInfo 类手动构造此格式字符串。

使用CurrencyPositivePatternCurrencyNegativePattern 上的文档(请参阅herehere),我整理了一些可行但可能需要一些调整的东西:

string GetCurrencyFormat(CultureInfo culture)
{            
    //we'll use string.Format later to replace {0} with the currency symbol
    //and {1} with the number format
    string[] negativePatternStrings = { "({0}{1})", "-{0}{1}", "{0}-{1}", "{0}{1}-", "({1}{0})",
                            "-{1}{0}", "{1}-{0}", "{1}{0}-", "-{1} {0}", "-{0} {1}",
                            "{1} {0}-", "{0} {1}-", "{0} -{1}", "{1}- {0}", "({0} {1})",
                            "({1} {0})" };
    string[] positivePatternStrings = { "{0}{1}", "{1}{0}", "{0} {1}", "{1}{0}" };

    var numberFormat = culture.NumberFormat;

    //Generate 0's to fill in the format after the decimal place
    var decimalPlaces = new string('0', numberFormat.CurrencyDecimalDigits);

    //concatenate the full number format, e.g. #,0.00
    var fullDigitFormat = $"#{numberFormat.CurrencyGroupSeparator}0{numberFormat.CurrencyDecimalSeparator}{decimalPlaces}";

    //use string.Format on the patterns to get the positive and 
    //negative formats
    var positiveFormat = string.Format(positivePatternStrings[numberFormat.CurrencyPositivePattern],  
                                       numberFormat.CurrencySymbol, fullDigitFormat);
    var negativeFormat = string.Format(negativePatternStrings[numberFormat.CurrencyNegativePattern], 
                                       numberFormat.CurrencySymbol, fullDigitFormat);

    //finally, return the full format
    return $"{positiveFormat};{negativeFormat}";
}

例如,对于en-US,这将返回$#,0.00;($#,0.00),对于en-GB,则返回£#,0.00;-£#,0.00

【讨论】:

  • 谢谢KMoussa。这非常接近我想要的。它似乎适用于符号出现在数字之前的货币。但是,某些文化格式要求在数字后使用货币符号,例如:“#,0 'CA$';(#,0 'CA$')”。我希望 .NET 有办法只返回它正在使用的东西(考虑到文化),但看起来我必须做这样的事情才能完成工作!
  • 我确信还有其他我不知道的特定于文化的细微差别 - 不仅仅是货币符号的位置。这就是为什么我希望框架(考虑到这些细微差别,考虑到文化)会吐出它正在使用的格式字符串。
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
相关资源
最近更新 更多