theNoobGuy
theNoobGuy

Reputation: 1636

C# - Changing the Amount of Decimals

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

Upvotes: 1

Views: 624

Answers (3)

colithium
colithium

Reputation: 10327

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
}

Upvotes: 2

Petar Ivanov
Petar Ivanov

Reputation: 93030

        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);

Upvotes: 2

Microfed
Microfed

Reputation: 2890

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.

Upvotes: 0

Related Questions