1

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...

theNoobGuy
  • 1,636
  • 6
  • 29
  • 45
  • 1
    Read the string as a line, then use the split method of your string object then use http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx – Pepe Jul 27 '11 at 03:41
  • 1
    [Check this answer](http://stackoverflow.com/questions/257005/how-do-you-round-a-number-to-two-decimal-places-in-c) – Jim D'Angelo Jul 27 '11 at 03:42

3 Answers3

2

You can use:

string[] piecesStringArray = myLine.Split('\t');  //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);
//Now recreate your string

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:

string[] myLines = entireFileText.Split(Environment.NewLine);
for(string myLine in myLines)
{
     //Previous code
}
colithium
  • 10,269
  • 5
  • 42
  • 57
  • Note to OP: you can add an extra arguement to Math.Round to specify how many decimal places you want to use – Pepe Jul 27 '11 at 03:45
  • Forgot to mention that, thanks. Feel free to edit my answer to make it better – colithium Jul 27 '11 at 03:46
  • @Colithium: I have updated my question for a multi-line file/`RichTextBox`... What would change in the code? – theNoobGuy Jul 27 '11 at 04:17
  • I added how to split the lines apart. I'm assuming you know about StringBuilder which is what you'd use to reconstruct the string that gets assigned to the rtb. – colithium Jul 27 '11 at 04:21
  • @colithium: I am not too familiar with StringBuilder actually.. heh. I will post my code above with what I was attempting to do and failing at.. POSTED – theNoobGuy Jul 27 '11 at 04:32
2
        string s = @"
TEXT  MORETEXT  227.905  174.994  180  1111
TEXT  MORETEXT  227.905  174.994  180  1111";

        string newS = Regex.Replace(s,
               "[0-9]+(.[0-9]+)?",
               m => string.Format("{0:0.##}", double.Parse(m.Value)), 
               RegexOptions.Singleline);
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • @Peter: If I had multiple lines, how could I go about using Regex to do this? – theNoobGuy Jul 27 '11 at 03:56
  • You would add `RegexOptions.Singleline` option. – Petar Ivanov Jul 27 '11 at 04:01
  • A little optimization: change "[0-9]+(.[0-9]+)?" with "\d+.\d{3}\d*" since you need to process only numbers which have at least 3 decimals. – Sasha Jul 27 '11 at 04:07
  • @Petar: This does not seem to be working properly. It gives me the same values and does not make it so there is only 2 decimals. – theNoobGuy Jul 28 '11 at 17:58
  • @theNoobGuy: not sure what you are doing, but the code above works - just ran it. It results in: "TEXT MORETEXT 227.91 174.99 180 1111 \n TEXT MORETEXT 227.91 174.99 180 1111" – Petar Ivanov Jul 28 '11 at 22:40
0

You need to follow these steps:

  1. Parse the string to get a string value of digits;
  2. Cast the strings of digits to the numeric types;
  3. Conduct manipulations with numbers;
  4. Generate a new string.

Each step is a different problem.

To solve the first problem you need to know the format of string. Always there are the numbers in this order? To parse a string you can use regular expressions or methods of a class Array (Split).

After extracting the digits from a string you can use methods of the Math class to round numbers. You can write your own version.

Microfed
  • 2,832
  • 22
  • 25