在 C# 中格式化带有有效数字的数字
我有一些十进制数据,正在将其推送到要查看的 SharePoint 列表中。 我想根据我对具体计算的了解来限制结果数据中显示的有效数字的数量。 有时它会是3,所以12345会变成12300,0.012345会变成0.0123。 偶尔会是 4 或 5。有什么方便的方法来处理这个吗?
I have some decimal data that I am pushing into a SharePoint list where it is to be viewed. I'd like to restrict the number of significant figures displayed in the result data based on my knowledge of the specific calculation. Sometimes it'll be 3, so 12345 will become 12300 and 0.012345 will become 0.0123. Occasionally it will be 4 or 5. Is there any convenient way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
请参阅:RoundToSignificantFigures “P爸爸”。
我把他的方法与我喜欢的另一种方法结合起来。
在 TSQL 中舍入到有效数字要容易得多,其中舍入方法基于舍入位置,而不是小数位数 - .Net math.round 就是这种情况。 您可以将 TSQL 中的数字四舍五入到负数,这将四舍五入为整数 - 因此不需要缩放。
另请参阅此其他线程。 Pyrolistical的方法很好。
对我来说,问题的尾随零部分似乎更像是字符串操作,因此我包含了一个 ToString() 扩展方法,该方法将在必要时填充零。
See: RoundToSignificantFigures by "P Daddy".
I've combined his method with another one I liked.
Rounding to significant figures is a lot easier in TSQL where the rounding method is based on rounding position, not number of decimal places - which is the case with .Net math.round. You could round a number in TSQL to negative places, which would round at whole numbers - so the scaling isn't needed.
Also see this other thread. Pyrolistical's method is good.
The trailing zeros part of the problem seems like more of a string operation to me, so I included a ToString() extension method which will pad zeros if necessary.
这可能会起作用:
将 G3 更改为 G4 会产生最奇怪的结果。
它似乎将有效数字四舍五入?
This might do the trick:
Changing the G3 to G4 produces the oddest result though.
It appears to round up the significant digits?
我最终从 http://ostermiller.org/utils/SignificantFigures.java.html 获取了一些代码。 它是用 java 编写的,所以我进行了快速搜索/替换和一些重新格式化以进行 C# 构建。 它似乎很适合我的重要数字需求。 FWIW,我删除了他的 javadoc 注释以使其在这里更加简洁,但原始代码的文档记录得相当好。
I ended up snagging some code from http://ostermiller.org/utils/SignificantFigures.java.html. It was in java, so I did a quick search/replace and some resharper reformatting to make the C# build. It seems to work nicely for my significant figure needs. FWIW, I removed his javadoc comments to make it more concise here, but the original code is documented quite nicely.
以下代码不太符合规范,因为它不会尝试将任何内容四舍五入到小数点左侧。 但它比这里介绍的任何其他内容(到目前为止)都要简单。 我很惊讶 C# 没有内置方法来处理这个问题。
The following code doesn't quite meet the spec, since it doesn't try to round anything to the left of the decimal point. But it's simpler than anything else presented here (so far). I was quite surprised that C# doesn't have a built-in method to handle this.
我通过快速搜索发现这篇文章在上面。 基本上,这个转换为一个字符串,并一次一个地遍历该数组中的字符,直到达到最大值。 意义。 这行得通吗?
I found this article doing a quick search on it. Basically this one converts to a string and goes by the characters in that array one at a time, till it reached the max. significance. Will this work?
我有一个关于计算数字的有效数字的简短答案。 这是代码& 测试结果...
使用的数学类如下:
有更短代码的答案吗?
I have a shorted answer to calculating significant figures of a number. Here is the code & the test results...
The Maths class used is as follows:
Any answer with a shorter code?
您可以通过使用 Decimal 上的 GetBits 方法并利用 BigInteger 执行掩码来获得优雅的位完美舍入。
一些utils
主要功能
测试用例0
测试用例1
You can get an elegant bit perfect rounding by using the GetBits method on Decimal and leveraging BigInteger to perform masking.
Some utils
The main function
test case 0
test case 1
此方法非常简单,适用于任何数字,无论是正数还是负数,并且仅使用单个超越函数 (Log10)。 唯一的区别(可能/可能无关紧要)是它不会舍入整数部分。 然而,这对于您知道限制在一定范围内的货币处理来说是完美的,因为您可以使用双精度数比慢得可怕的十进制类型进行更快的处理。
This method is dead simple and works with any number, positive or negative, and only uses a single transcendental function (Log10). The only difference (which may/may-not matter) is that it will not round the integer component. This is perfect however for currency processing where you know the limits are within certain bounds, because you can use doubles for much faster processing than the dreadfully slow Decimal type.
我记得“有效数字”是指点分隔符之后的位数,因此 0.012345 的 3 位有效数字将是 0.012 而不是 0.0123,但这对于解决方案来说并不重要。
我也理解,如果数字 > ,您希望在一定程度上“无效”最后一位数字。 1. 您写下 12345 将变为 12300,但我不确定您是否希望 123456 变为 1230000 还是 123400? 我的解决方案是最后一个。 如果您只有几个变体,您当然可以创建一个小的初始化数组,而不是计算因子。
亲切的问候
卡斯滕
As I remember it "significant figures" means the number of digits after the dot separator so 3 significant digits for 0.012345 would be 0.012 and not 0.0123, but that really doesnt matter for the solution.
I also understand that you want to "nullify" the last digits to a certain degree if the number is > 1. You write that 12345 would become 12300 but im not sure whether you want 123456 to become 1230000 or 123400 ? My solution does the last. Instead of calculating the factor you could ofcourse make a small initialized array if you only have a couple of variations.
Kind regards
Carsten