title: Formatting Serial Input to Text Box in C#
body: <p>I am trying to get the weight from a Toledo 8510 scale to display in a text box where it will be captured and sent to a SQL Server Database.  I have mashed together code from a couple of sources and have gotten the scale to communicate and display output in the text box, but where the scale display says 5.65, the textbox displays 56500000. When there is no weight on the scale it displays 0.  The output from the scale is in ASCII, and when I use the SerialPortListener from Code Project or Putty the output looks like this:  &lt;' 00565 00000.  I have code in my solution to delete the &lt;' and the spaces so I'm sure those last five zeros are the second half of the ascii string.</p>
  
  <p>This is for scoring a fishing tournament run by a local charity and we can't afford a professional, so I'm learning to code as I go.  Any help would be greatly appreciated.  My code is below:</p>
  
  <pre><code>private void UserInitialization()
  {
      _spManager = new SerialPortManager();
      SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;
      serialSettingsBindingSource.DataSource = mySerialSettings;
      ComPrtDrpDnBx.DataSource = mySerialSettings.PortNameCollection;
      BaudRtDrpdnBx.DataSource = mySerialSettings.BaudRateCollection;
      DataBtsDrpDnBx.DataSource = mySerialSettings.DataBitsCollection;
      ParityDrpDnBx.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
      StpBtsDrpDnBx.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));
  
      _spManager.NewSerialDataRecieved += new EventHandler&lt;SerialDataEventArgs&gt;(_spManager_NewSerialDataRecieved);
      //this.FormClosing += new FormClosingEventHandler(WeighInFrm_FormClosing);
  }
  
  private void WeighInForm_FormClosing(object sender, FormClosingEventArgs e)
  {
      _spManager.Dispose();
  }
  
  void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
  {
      if (this.InvokeRequired)
      {
          // Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
          this.BeginInvoke(new EventHandler&lt;SerialDataEventArgs&gt;(_spManager_NewSerialDataRecieved), new object[] { sender, e });
          return;
      }
  
      int maxTextLength = 9; // maximum text length in text box
      if (CapWeghtTxtBx.TextLength &gt; maxTextLength)
          CapWeghtTxtBx.Text = CapWeghtTxtBx.Text.Remove(0, CapWeghtTxtBx.TextLength - maxTextLength);
      // This application is connected to a Scale sending ASCII characters, so data is converted to text
      string str = Encoding.ASCII.GetString(e.Data);
      string newString = Regex.Replace(str, "[^.0-9]", ""); //Remove the Caret symbol and any spaces
      CapWeghtTxtBx.AppendText(newString);
      CapWeghtTxtBx.Select(CapWeghtTxtBx.TextLength, 0);
      CapWeghtTxtBx.ScrollToCaret();
      CapWeghtTxtBx.Text = string.Format("{0:0}", double.Parse(CapWeghtTxtBx.Text)); //formats the text
  
      }
  
  
      private void CapWeghtTxtBx_TextChanged(object sender, EventArgs e)            
      {
  
      }
  </code></pre>