繁体   English   中英

C# - 更改小数的数量

[英]C# - Changing the Amount of Decimals

我有一个看起来像这样的字符串:

TEXT  MORETEXT  227.905  174.994  180  1111

我很好奇如何将小数改为只有2个位置。

所以不是上面的字符串,我希望它是:

TEXT  MORETEXT  227.90  174.99  180  1111 #rounding down

要么

TEXT  MORETEXT  227.91  174.99  180  1111 #rounding up

有谁知道如何获取此字符串并将值更改为小数,然后减少小数?


编辑:

所以,如果我有一个文件读取:( 注意:该文件有多行)

TEXT  MORETEXT  227.905  174.994  180  1111
TEXT  MORETEXT  227.905  174.994  180  1111
TEXT  MORETEXT  227.905  174.994  180  1111

我希望它像这样进入RichTextBox

TEXT  MORETEXT  227.90  174.99  180  1111
TEXT  MORETEXT  227.90  174.99  180  1111
TEXT  MORETEXT  227.90  174.99  180  1111

我怎么能这样做?


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

在将其上传到ListBox之前,我试图减少RTB中每行的十进制值。 但是我在挣扎......

您可以使用:

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

当然你应该添加错误检查等。 如果你想反对传统的舍入,请查看Math.Floor()和Math.Ceil()。 您需要将数字除以10的幂才能使其正常工作。

要获得myLine,您可以使用:

string[] myLines = entireFileText.Split(Environment.NewLine);
for(string myLine in myLines)
{
     //Previous code
}
        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);

您需要按照以下步骤操作:

  1. 解析字符串以获取数字的字符串值;
  2. 将数字字符串转换为数字类型;
  3. 用数字进行操纵;
  4. 生成一个新字符串。

每一步都是一个不同的问题。

要解决第一个问题,您需要知道字符串的格式。 总是有这个顺序的数字? 要解析字符串,可以使用类Array(Split)的正则表达式或方法。

从字符串中提取数字后,您可以使用Math类的方法来舍入数字。 您可以编写自己的版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM