Convert.Double(string)工作错误

时间:2014-11-26 12:57:38

标签: c# .net string double

我尝试通过串口从mettler toledo称重机读取一些重量值。当我将串口的字符串转换为双倍时,将点放到错误的位置。它将144.95转换为14495.0。如何将此字符串转换为正确的double值?

if (this.InvokeRequired)
{
    // Using this.Invoke causes deadlock when closing serial port, 
    // and BeginInvoke is good practice anyway.
    this.BeginInvoke(
        new EventHandler<SerialDataEventArgs>(spManager_NewSerialDataRecieved), 
        new object[] { sender, e });
    return;
}
string pattern = @"\s.(\d+\.\d{2})(\sg\r\n(S\sS\s.)?)?$";
string data = Encoding.ASCII.GetString(e.Data);
MatchCollection mc = Regex.Matches(data, pattern);
if (mc.Count > 0)
{
    if (tartim)
    {
        double weight = Convert.ToDouble(mc[0].Groups[1].Value); //Wrong conversion
        if (weight > weightLowerLimit && weight < weightUpperLimit)
        {
            MoveToNextSlot();
        }
    }
}

0 个答案:

没有答案