avoid extra zeros in number formatting in C#

2019-07-21 10:43发布

(First of all: this is not a duplicate of Format numbers with floating points and sign in textbox in C#)

I am using the solution from dodald (https://stackoverflow.com/a/27510646/3179989) for formatting numbers with floating points and sign to make them aligned based on the floating point:

textbox1.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number1);
textbox2.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number2);
textbox3.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number3);
textbox4.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number4);
textbox5.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number5);

It works perfectly for numbers are like -1.23456 (results in "- 1.23456"), however if the number is 1.2, then this will change it to "+ 1.20000".

Questions: Is there anyway to avoid any extra zeros? How can I add a space at the end of the text by using the formatting parameters and not using (text + " ")?

Thanks in advance

EDIT: to clarify this here is what I am looking for:

-123.123456
+  1.123456
-  0.123456
-  0.123
+  1.1
- 12.123456

I have multiple textboxes that are vertically aligned. I want the numbers to be displayed the way is presented above where the position of the floating point is always fixed and vertically aligned. The solution provided by dodald (https://stackoverflow.com/a/27510646/3179989) works just for the number with five decimal numbers like -1.23456 (results in "- 1.23456"), however if the number is 1.2, then his solution will change it to "+ 1.20000". I'd like to remove the added zeros to the string.

2条回答
狗以群分
2楼-- · 2019-07-21 11:19

You can use this code by using string builder and you will find that all zeros on the right side of decimal point deleted and this is shown before (1555454545445.20000000000000000) and after (1555454545445.2) in console:

    static void Main(string[] args)
    {
        string input = "1555454545445.20000000000000000";
        StringBuilder sb = new StringBuilder(input);
        for (int i = 0; i < input.Length-2; i++)
        {
            if (input[i] == '.')
            {
                if (input[input.Length - 1] == '0')
                {
                    for (int x = 0; x < input.Length - i; i++)
                    {
                        if (sb[sb.Length - 1] == '0') 
                        {
                            sb = sb.Remove((sb.Length - 1), 1);
                        }
                    }
                }
            }
        }
        Console.WriteLine(input);
        Console.WriteLine(sb.ToString());
    }
查看更多
该账号已被封号
3楼-- · 2019-07-21 11:22

You can also use this code for removing any additional zeros on the right of the decimal point by using regular expression:

class MainProgram
{
    public static void Main(string[] args)
    {
        decimal DNumber = 1223456.45600000M;
        Console.WriteLine("Original Decimal Number = {0}, Decimal Number Without Zeros = {1}", DNumber, DNumber.ToString("#.##############"));
        Console.Read();
    }
}
查看更多
登录 后发表回答