I have a string that looks like this:
TEXT MORETEXT 227.905 174.994 180 1111
I am curious on how I can change the decimal to only have 2 places.
So instead of the string above, I would like it to be:
TEXT MORETEXT 227.90 174.99 180 1111 #rounding down
OR
TEXT MORETEXT 227.91 174.99 180 1111 #rounding up
Does anyone know how to grab this string and change the values to a decimal and then trim the decimal down?
EDIT:
So if I had a file that reads: (Note: The file has multiple lines)
TEXT MORETEXT 227.905 174.994 180 1111
TEXT MORETEXT 227.905 174.994 180 1111
TEXT MORETEXT 227.905 174.994 180 1111
and I want it to go into a RichTextBox like this:
TEXT MORETEXT 227.90 174.99 180 1111
TEXT MORETEXT 227.90 174.99 180 1111
TEXT MORETEXT 227.90 174.99 180 1111
How could I do that?
EDIT2:
string[] listOneLines = null;
string[] listTwoLines = null;
string[] listUserLines = null;
// Splits the lines in the rich text boxes
listOneLines = removePKG1EndingsRichTextBox.Text.Split('\n');
listTwoLines = removePKG2EndingsRichTextBox.Text.Split('\n');
listUserLines = removeUserEndingsRichTextBox.Text.Split('\n');
string[] myLines = removePKG1EndingsRichTextBox.Text.Split('\n');
StringBuilder sb = new StringBuilder();
foreach(string myLine in myLines)
{
string[] piecesStringArray = removePKG1EndingsRichTextBox.Text.Split(' '); //Assuming those are tabs
double d1 = Convert.ToDouble(piecesStringArray[2]);
double d2 = Convert.ToDouble(piecesStringArray[3]);
double round1 = Math.Round(d1, 2);
double round2 = Math.Round(d2, 2);
sb.AppendLine(piecesStringArray[0] + piecesStringArray[1] + round1 + round2 + piecesStringArray[4] + piecesStringArray[5]);
}
listOneLines = sb.ToString(); #does not work..
// Set the selection mode to multiple and extended.
placementOneListBox.SelectionMode = SelectionMode.MultiExtended;
placementTwoListBox.SelectionMode = SelectionMode.MultiExtended;
userDefinedListBox.SelectionMode = SelectionMode.MultiExtended;
// Shutdown the painting of the ListBox as items are added.
placementOneListBox.BeginUpdate();
placementTwoListBox.BeginUpdate();
userDefinedListBox.BeginUpdate();
// Display the items in the listbox.
foreach (var item in listOneLines)
placementOneListBox.Items.Add(item);
foreach (var item2 in listTwoLines)
placementTwoListBox.Items.Add(item2);
foreach (var item3 in listUserLines)
userDefinedListBox.Items.Add(item3);
// Allow the ListBox to repaint and display the new items.
placementOneListBox.EndUpdate();
placementTwoListBox.EndUpdate();
userDefinedListBox.EndUpdate();
I was trying to reduce the decimal values on each line in the RTB before I upload it to the ListBox. However I am struggling…
You can use:
Of course you should add error checking and the like. If you want to go against conventional rounding, check out Math.Floor() and Math.Ceil(). You’ll need to scale the number by powers of ten to get it working right.
To get myLine you can use: