.NET中的货币格式在金额和货币之间给出了空格

问题描述 投票:4回答:6

我在C#中遇到货币格式问题。我正在使用框架2.0。

当我使用这段代码时:

CultureInfo culture = new CultureInfo("fr-FR", false);
NumberFormatInfo numberFormatInfo = (NumberFormatInfo)culture.NumberFormat.Clone();
numberFormatInfo.CurrencySymbol = "CHF";

price.Value.ToString("C", numberFormatInfo)似乎给了我一个在金额和货币之间有空格的字符串。那太糟了!我绝对需要一个不间断的空间!到底是怎么回事?我错过了格式属性还是C#标准?

谢谢你的帮助!

所以基本上你想要price.Value.ToString(“C”,numberFormatInfo).Replace('','\ u00A0');?至少应该是非破坏空间的代码。 - 科拉克

与上面的评论员完全相同,但使用asci-values代替; > price.Value.ToString(“C”,numberFormatInfo).Replace((char)32,(char)160); (160是很多>更容易记住,至少对我来说:)) - 弗林德伯格

c# .net formatting format currency
6个回答
4
投票

根据我对@Corak似乎分享的问题的解释添加答案。

// Convert "breaking" spaces into "non-breaking" spaces (ie the html  )
price.Value.ToString("C", numberFormatInfo).Replace((char) 32, (char) 160);

对unicode做同样的事情(由@ Corak的链接提供):

// Convert "breaking" spaces into "non-breaking" spaces without int cast to char
price.Value.ToString("C", numberFormatInfo).Replace(' ', '\u00A0');

顺便说一下(roslyn repl):

> '\u00A0' == (char) 160
true

如果您打算使用它,还可以获得扩展方法:

public static class StringExtensions 
{// CurrencyType is your currency type, guessing double or decimal?
 public static string ToCurrencyString(this CurrencyType value, IFormatInfo format)
 {
    return value.ToString("C", format).Replace((char) 32, (char) 160);
 }
}

6
投票

使用:

numberFormatInfo.CurrencyPositivePattern = 1;

对于价值1格式是n$,其中$是货币符号,在你的情况下,它的CHF

Formatting Types - MSDN

CurrencyNegativePattern或CurrencyPositivePattern属性,返回一个确定以下内容的整数:

  • 货币符号的位置。
  • 负值是由前导负号,尾随负号还是括号表示。
  • 是否在数值和货币符号之间出现空格。

请尝试以下代码:

    CultureInfo culture = new CultureInfo("fr-FR", false);
    NumberFormatInfo numberFormatInfo = (NumberFormatInfo)culture.NumberFormat.Clone();
    numberFormatInfo.CurrencySymbol = "CHF";
    numberFormatInfo.CurrencyPositivePattern = 1;
    decimal d = 123.23M;
    var temp = d.ToString("C", numberFormatInfo);

输出:

123,23CHF

4
投票

你可以替换它。

price.ToString("C", numberFormatInfo).Replace(" ", "")

或者更好地将NumberFormatInfo.CurrencyPositivePattern设置为1

numberFormatInfo.CurrencySymbol = "CHF";
numberFormatInfo.CurrencyPositivePattern = 1;

完整的例子;

CultureInfo culture = new CultureInfo("fr-FR", false);
NumberFormatInfo numberFormatInfo = (NumberFormatInfo)culture.NumberFormat.Clone();
numberFormatInfo.CurrencySymbol = "CHF";
numberFormatInfo.CurrencyPositivePattern = 1;
Console.WriteLine((1.5M).ToString("C", numberFormatInfo));

输出将是;

1,50CHF

这里有一个DEMO

来自Formatting Types

CurrencyNegativePattern或CurrencyPositivePattern属性,返回一个确定以下内容的整数:

  • 货币符号的位置。
  • 负值是由前导负号,尾随负号还是括号表示。
  • 是否在数值和货币符号之间出现空格。

3
投票

您可以配置CurrencyPositivePattern并将其设置为适当的值。

// whatever code you had
NumberFormatInfo numberFormatInfo = (NumberFormatInfo)culture.NumberFormat.Clone();
numberFormatInfo.CurrencyPositivePattern = 1; // will format like 25.00CHF

1
投票

似乎默认情况下它包含的空间涉及其他货币而不是$

double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

Console.WriteLine(value.ToString("C3", 
                  CultureInfo.CreateSpecificCulture("da-DK")));
// The example displays the following output on a system whose 
// current culture is English (United States): 
//       $12,345.68 
//       $12,345.679 
//       kr 12.345,679

如果你想更换空间,你可以像Soner说的那样使用

numberString.ToString("C", numberFormatInfo).Replace(" ", "");

0
投票

在global.asax中,您可以全局更改文化并选择删除这样的空间:

CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("fr-FR"); //Get you own culture
cultureInfo.NumberFormat.CurrencyPositivePattern = 1; //To remove the space
Thread.CurrentThread.CurrentUICulture = cultureInfo; //Apply globaly to your application
Thread.CurrentThread.CurrentCulture = cultureInfo; //Apply globaly to your application
© www.soinside.com 2019 - 2024. All rights reserved.