這是做的非常討厭的方式:
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;
}
是十進制永遠只有第十?因爲如果你有147.258和147,258 ......你沒有足夠的信息來知道你的意思是147.258還是147,258.000 – Ryan
什麼是'123,456'? – SimpleVar
這是固定寬度的數據,你可以依賴小數(如果有的話)總是在同一個位置? – DVK