避免在C#中数字格式的额外零 [英] avoid extra zeros in number formatting in C#

查看:251
本文介绍了避免在C#中数字格式的额外零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(首先:这不是使用浮点数格式化数字并在C#中登录文本框

我正在使用dodald的解决方案( https://stackoverflow.com / a / 27510646/3179989 )用浮点格式化数字并签名,使它们基于浮点对齐:

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

完美的数字就像-1.23456(结果是 - 1.23456),但是如果数字是1.2,那么这将改变为+ 1.20000。

问题:
是否有避免任何额外的零?
如何使用格式化参数在文本末尾添加空格而不是使用(text +)?

/ p>

编辑:澄清这里这是我正在寻找:

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

我有多个垂直对齐的文本框。我希望数字以浮动点的位置总是固定和垂直对齐的方式显示。由dodald提供的解决方案( https://stackoverflow.com/a/27510646/3179989 )只适用于数字五个十进制数字,如-1.23456(结果在 - 1.23456),但是如果数字是1.2,那么他的解决方案将改变为+ 1.20000。我想删除添加到字符串的零。

解决方案

您可以通过使用字符串生成器来使用此代码,您将发现小数点右侧的所有零被删除(1555454545445.20000000000000000)和之后(1555454545445.2)在控制台中显示:

pre $ static $ Main $ string $ $ b {
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 { (sb.Length - 1)=='0')
{
sb = sb.Remove((sb.Length - 1),1);
}
}
}
}
}
Console.WriteLine(input);
Console.WriteLine(sb.ToString());
}


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

解决方案

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

这篇关于避免在C#中数字格式的额外零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆