最有效的/右实践修剪无意义的零在小数的末尾小数、末尾、最有效、无意义

2023-09-08 09:48:11 作者:大众爹

这是多次重复SO,但我想明确说明我的问题。

This is so many times repeated at SO, but I would want to state my question explicitly.

How is a decimal which would look like 2.0100 "rightly" presented to the user as 
another "decimal" 2.01?

我看到那么,输入是一个字符串2.0100很多问题,需要一个小数2.01出它和问题,他们需要十进制2.0100重新psented为字符串2.01$ P $的地方。所有这一切都可以通过基本string.Trim,decimal.Parse等来实现这些都是一些办法如下:

I see a lot of questions on SO where the input is a string "2.0100" and need a decimal 2.01 out of it and questions where they need decimal 2.0100 to be represented as string "2.01". All this can be achieved by basic string.Trim, decimal.Parse etc. And these are some of the approaches followed:

decimal.Parse(2.0100.ToString(G29))

使用#文字

很多的String.Format选项。

Many string.Format options.

正则表达式的各种选项

我自己一个人我用至今:

My own one I used till now:

if (2.0100 == 0)
    return 0;
decimal p = decimal.Parse(2.0100.ToString().TrimEnd('0'));
return p == 2.0100 ? p : 2.0100;

不过,我相信,必须有这样做的。Net(至少4)一些正确的方法其中涉及数字操作,而不是字符串操作。我要求的东西,不与数字作为字符串处理,因为我觉得这是不正确的方法来做到这一点。我想学习新的东西。而且会看上看到至少0.1秒的性能提升,因为我是从数据库中提取的数千个十进制值几万我的机会:)

But I believe there has to be some correct way of doing it in .Net (at least 4) which deals with numeric operation and not string operation. I am asking for something that is not dealing with the decimal as string because I feel that ain't the right method to do this. I'm trying to learn something new. And would fancy my chances of seeing at least .1 seconds of performance gain since I'm pulling tens of thousands of decimal values from database :)

问2:如果AINT present在.NET中,这是最有效的字符串的方法来获取小数点一个presentable值

Question 2: If it aint present in .Net, which is the most efficient string method to get a presentable value for the decimal?

编辑:我不只是想十进制为presented给用户。在这种情况下,我可以把它作为一个字符串。我不希望它为十进制回来。我将在后面的十进制值来处理。所以要通过的ToString方法,我首先需要将其转换为字符串,然后再解析为十进制。我要寻找的东西,不处理字符串类。有些选项转换小数0.20100十进制.201?

I do not just want a decimal to be presented it to users. In that case I can use it as a string. I do want it as decimal back. I will have to process on those decimal values later. So going by ToString approach, I first needs to convert it to string, and then again parse it to decimal. I am looking for something that doesn't deal with String class. Some option to convert decimal .20100 to decimal .201?

推荐答案

好了,我回答我自己,我得到了一个解决方案。

Ok, so I'm answering for myself, I got a solution.

return d / 1.00000000000000000000000000000m

这只是它。我做了一些基准测试,以及(psented的意见时$ P $的模式,而不是平均):

That just does it. I did some benchmarking as well (time presented as comments are mode, not mean):

方法:

    internal static double Calculate(Action act)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();
        act();
        sw.Stop();

        return sw.Elapsed.TotalSeconds;
    }

考生:

return decimal.Parse(string.Format("{0:0.#############################}", d));
//0.02ms

return decimal.Parse(d.ToString("0.#############################"));
//0.014ms

if (d == 0)
    return 0;

decimal p = decimal.Parse(d.ToString().TrimEnd('0').TrimEnd('.'));
return p == d ? p : d;
//0.016ms

return decimal.Parse(d.ToString("G29"));
//0.012ms

return d / 1.00000000000000000000000000000m;
//0.007ms

不用支付正则表达式的选项。我不意思是说表现使得有很大的区别。我只是拉5K到20K行的时间。不过还是很高兴知道更简单和更清洁的替代字符串的方式存在。

Needless to cover regex options. I dont mean to say performance makes a lot of difference. I'm just pulling 5k to 20k rows at a time. But still it's nice to know a simpler and cleaner alternative to string approach exists.

如果你喜欢的答案,请您重定向票对这里 或此处或此处 。

If you liked the answer, pls redirect your votes to here or here or here.