我正在开发一个需要解析文件数据并将其存储到MySQL数据库的项目 该文件具有多种格式的以下字符串
Ava. bytes -> 147.258.369
Ava. bytes -> 147.258.369,5
Ava. bytes -> 147,258,369
Ava. bytes -> 147,258,369.5
将这些格式转换为
的最佳方式是什么?Ava. bytes -> 147.258.369 => 147258369.0
Ava. bytes -> 147.258.369,5 =>147258369.5
Ava. bytes -> 147,258,369 => 147258369.0
Ava. bytes -> 147,258,369.5 =>147258369.5
谢谢你!
答案 0 :(得分:0)
我基本上是通过评论解释解决方案, 所以你可以单独阅读评论并获得想法。
注意我使用decimal
作为最终结果。但如果您愿意,可以使用float
或double
。一般方法仍然有效。
string str = "123,456.7";
// Check if its a simple integer (no floating point or thousands separators)
if (str.Length <= 3)
return (decimal)int.Parse(str);
// Get the floating-point character from the string
char floatingPointChar = str[str.Length - 2];
// Based on the floating-point character,
// we assure the string is in a standard format of "000,000.0"
switch (floatPointChar)
{
case '.':
// If the floating point is a .
// the number is already in a standard format
break;
case ',':
// put ~ as a temporary floating-point character
str = str.Replace(',', '~');
// turn dots into thousand separators
str = str.Replace('.', ',');
// put . as floating-point character
str = str.Replace('~', '.');
break;
default:
// There is actually no floating point,
// so just make sure thousand separators are ,
str = str.Replace('.', ',');
break;
}
// Now our string is in a standard format
// , for thousands separators
// . for floating point
return decimal.Parse(str, CultureInfo.InvariantCulture);
编辑:我完全忽略了千位分隔符可以完全省略的事实。请参阅null的答案以获得更多灵感
答案 1 :(得分:0)
您可以使用正则表达式删除任意后跟三位数的点或逗号,并使用字符串替换以替换最后一位小数逗号(如果有的话):
string n = "147.258.369,5";
var r = new Regex(@"[.,](?=\d{3})");
string n2 = r.Replace(n, "").Replace(',', '.');
修改强>
如果要将其解析为十进制,则该字符串在InvariantFormat中(但可能您需要字符串,因为.0很重要。)
包含最终.0
:
if (n2.Length < 2 || n2[n2.Length - 2] != '.')
{
n2 = n2 + ".0";
}
答案 2 :(得分:0)
这是一种非常讨厌的方式:
private static string FormatIntString(string input)
{
if (input.IndexOf('.') != input.LastIndexOf('.'))
{
if (input.Contains(","))
{
//this case-> Ava.bytes -> 147.258.369,5 =>147258369.5
return DoFormat(input.Replace(".", "").Replace(',', '.'));
}
else
{
// this case-> Ava.bytes -> 147.258.369 => 147258369.0
return DoFormat(input.Replace(".", ""));
}
}
else
{
if (input.Contains('.'))
{
//this case -> Ava.bytes -> 147,258,369.5 =>147258369.5
return DoFormat(input.Replace(",", ""));
}
else
{
//this case -> Ava.bytes -> 147,258,369 => 147258369.0
return DoFormat(input.Replace(",", ""));
}
}
}
public static string DoFormat(string myNumber)
{
var s = string.Format("{0:0.00}", myNumber);
if (s[s.Length-2] != '.')
return (myNumber + ".0");
else
return s;
}
请注意,这仅适用于至少包含两个','或'。'的字符串。
简化代码:
private static string FormatIntString(string input)
{
if (input.IndexOf('.') != input.LastIndexOf('.'))
if (input.Contains(","))
return DoFormat(input.Replace(".", "").Replace(',', '.'));
else
return DoFormat(input.Replace(".", ""));
else
if (input.Contains('.'))
return DoFormat(input.Replace(",", ""));
else
return DoFormat(input.Replace(",", ""));
}
public static string DoFormat(string myNumber)
{
var s = string.Format("{0:0.00}", myNumber);
if (s[s.Length - 2] != '.')
return (myNumber + ".0");
else
return s;
}