应用程序处理表示来自不同文化的小数的字符串。 例如,“1.1和”1,1“是相同的值。
我使用Decimal.TryParse
标志组合但无法达到我想要的结果。毕竟“1,1”变为“11”或“0”。
是否可以在一行代码中将这些字符串转换为十进制而不预先将“,”char替换为“。”或者玩NumberFormat.NumberDecimalSeparator
?
你如何处理这种情况?
提前谢谢!
答案 0 :(得分:32)
您可以创建在解析时使用的临时CultureInfo
对象。
// get a temporary culture (clone) to modify
var ci = CultureInfo.InvariantCulture.Clone() as CultureInfo;
ci.NumberFormat.NumberDecimalSeparator = ",";
decimal number = decimal.Parse("1,1", ci); // 1.1
答案 1 :(得分:11)
我找到了另一种方法。它看起来很奇怪,但对我来说效果很好。
因此,如果您不了解目标系统的文化,并且您不知道您将获得12.33或12,33的值,那么您可以执行以下操作
string amount = "12.33";
// or i.e. string amount = "12,33";
var c = System.Threading.Thread.CurrentThread.CurrentCulture;
var s = c.NumberFormat.CurrencyDecimalSeparator;
amount = amount.Replace(",", s);
amount = amount.Replace(".", s);
decimal transactionAmount = Convert.ToDecimal(amount);
答案 2 :(得分:6)
您有以下可能性:
答案 3 :(得分:4)
在调用Parse
时,您需要拥有正确的文化集,如下所示:
string s = "11,20";
decimal c1 = decimal.Parse(s, new CultureInfo("fr-FR"));
decimal c2 = decimal.Parse(s, new CultureInfo("en-AU"));
Console.WriteLine(c1);
Console.WriteLine(c2);
答案 4 :(得分:3)
以下是我的实施,任何好的想法?
/// <summary>
///
/// </summary>
public static class NumberExtensions
{
/// <summary>
/// Convert string value to decimal ignore the culture.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Decimal value.</returns>
public static decimal ToDecimal ( this string value )
{
decimal number;
string tempValue = value;
var punctuation = value.Where ( x => char.IsPunctuation ( x ) ).Distinct ( );
int count = punctuation.Count ( );
NumberFormatInfo format = CultureInfo.InvariantCulture.NumberFormat;
switch ( count )
{
case 0:
break;
case 1:
tempValue = value.Replace ( ",", "." );
break;
case 2:
if ( punctuation.ElementAt ( 0 ) == '.' )
tempValue = value.SwapChar ( '.', ',' );
break;
default:
throw new InvalidCastException ( );
}
number = decimal.Parse ( tempValue, format );
return number;
}
/// <summary>
/// Swaps the char.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="from">From.</param>
/// <param name="to">To.</param>
/// <returns></returns>
public static string SwapChar ( this string value, char from, char to )
{
if ( value == null )
throw new ArgumentNullException ( "value" );
StringBuilder builder = new StringBuilder ( );
foreach ( var item in value )
{
char c = item;
if ( c == from )
c = to;
else if ( c == to )
c = from;
builder.Append ( c );
}
return builder.ToString ( );
}
}
[TestClass]
public class NumberTest
{
/// <summary>
///
/// </summary>
[TestMethod]
public void Convert_To_Decimal_Test ( )
{
string v1 = "123.4";
string v2 = "123,4";
string v3 = "1,234.5";
string v4 = "1.234,5";
string v5 = "123";
string v6 = "1,234,567.89";
string v7 = "1.234.567,89";
decimal a1 = v1.ToDecimal ( );
decimal a2 = v2.ToDecimal ( );
decimal a3 = v3.ToDecimal ( );
decimal a4 = v4.ToDecimal ( );
decimal a5 = v5.ToDecimal ( );
decimal a6 = v6.ToDecimal ( );
decimal a7 = v7.ToDecimal ( );
Assert.AreEqual ( ( decimal ) 123.4, a1 );
Assert.AreEqual ( ( decimal ) 123.4, a2 );
Assert.AreEqual ( ( decimal ) 1234.5, a3 );
Assert.AreEqual ( ( decimal ) 1234.5, a4 );
Assert.AreEqual ( ( decimal ) 123, a5 );
Assert.AreEqual ( ( decimal ) 1234567.89, a6 );
Assert.AreEqual ( ( decimal ) 1234567.89, a7 );
}
/// <summary>
///
/// </summary>
[TestMethod]
public void Swap_Char_Test ( )
{
string v6 = "1,234,567.89";
string v7 = "1.234.567,89";
string a1 = v6.SwapChar ( ',', '.' );
string a2 = v7.SwapChar ( ',', '.' );
Assert.AreEqual ( "1.234.567,89", a1 );
Assert.AreEqual ( "1,234,567.89", a2 );
}
}
答案 5 :(得分:2)
很好,但仍然没有100%正确。 当你使用案例1:你自动地假设,','代表十进制数字。你应该至少检查它是否发生过多次,因此在这种情况下它会产生一个分离符号的组
case 1:
var firstPunctuation = linq.ElementAt(0);
var firstPunctuationOccurence = value.Where(x => x == firstPunctuation).Count();
if (firstPunctuationOccurence == 1)
{
// we assume it's a decimal separator (and not a group separator)
value = value.Replace(firstPunctuation.ToString(), format.NumberDecimalSeparator);
}
else
{
// multiple occurence means that symbol is a group separator
value = value.Replace(firstPunctuation.ToString(), format.NumberGroupSeparator);
}
break;
答案 6 :(得分:0)
使用TryParse两次并使用两种代表两种可能性的样式
如果只有一个返回值,请使用该值
如果两者都返回一个值,请使用绝对值中的较小值。
如果使用错误的样式,TryParse将为同时使用分组和十进制分隔符的数字返回0,但是如果您的字符串中没有分组分隔符(例如,数字小于1000),它将返回值in两种情况都可以,但“错误”的数字会更大(绝对值再次出现)