这是我为自己想出的解决方案。这已准备好作为命令提示符项目运行。如果没有,您需要清洁一些东西。希望这可以帮助。它接受几种输入格式,例如:1.234.567,89 1,234,567.89等
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Linq;
namespace ConvertStringDecimal
{
class Program
{
static void Main(string[] args)
{
while(true)
{
string input = Console.ReadLine();
double result = 0;
input = input.Replace(" ", "");
if (string.IsNullOrEmpty(input) == false)
{
if (input.Contains(",") && input.Contains("."))
{
int decimalpos = input.LastIndexOf(',') > input.LastIndexOf('.') ? input.LastIndexOf(',') : input.LastIndexOf('.');
input = input.Substring(0, decimalpos) + "|" + input.Substring(decimalpos + 1);
input = input.Replace(".", "").Replace(",", "").Replace("|", ".");
}
if (input.Contains(","))
{
input = input.Replace(',', '.');
}
if(input.Count(item => item == '.') > 1)
{
input = input.Replace(".", "");
}
if (double.TryParse(input, out result) == true)
{
result = Double.Parse(input, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, CultureInfo.InvariantCulture);
}
}
Console.WriteLine(result.ToString());
Console.WriteLine("----------------");
}
}
}
}