不进行舍入的十进制格式.net

发布于 2024-07-27 02:52:28 字数 750 浏览 3 评论 0原文

昨天我问了这个关于小数及其内部精度的一般问题。 这是一个关于我试图解决的场景的具体问题。

我在 SqlServer 中有一个输入为十进制(18,6)的列。 当我获取这些值时,创建的 .net 小数与数据库中的精度匹配。 它们看起来像这样:

1.100000
0.960000
0.939000
0.844400
0.912340

我需要根据这些规则呈现(ToString)这些值:

  • 始终显示非零。
  • 显示小数点后第 n 位或之前的尾随零。
  • 不显示小数点后第 n 位的尾随零。

所以,当 n 为 3 时,这就是我想要的:

1.100
0.960
0.939
0.8444
0.91234

现在,我编写了一些代码,ToString 是小数 - 删除所有尾随零,然后分析字符串查找小数点并计算小数位数以查看有多少个需要重新添加尾随零。 是否有更好的方法来完成此操作?

另外,我知道我在上面的问题中说了 ToString...但是如果我可以在离开数据访问层时修改小数,这样消费者总是得到具有适当精度的小数,那就更好了。 是否可以在不进行字符串操作的情况下对小数本身执行此操作?

Yesterday I asked this general question about decimals and their internal precisions. Here is a specific question about the scenario I'm trying to address.

I have column in SqlServer that is typed - decimal(18,6).
When I fetch these values, the .net decimals that are created match the precision in the database. They look like this:

1.100000
0.960000
0.939000
0.844400
0.912340

I need to present (ToString) these values according to these rules:

  • Non-zeroes are always shown.
  • Trailing zeroes on or before the nth decimal place are shown.
  • Trailing zeroes after the nth decimal place are not shown.

So, this is what I want when n is 3:

1.100
0.960
0.939
0.8444
0.91234

Now, I have written some code that ToString's the decimal - removing all trailing zeroes, and then analyzes the string looking for a decimal point and counts the number of decimal places to see how many trailing zeroes need to be added back on. Is there a better way to accomplish this?

Also, I know I said ToString in the question above... but if I could modify the decimals on their way out of my data-access layer, such that consumers always get decimals with the proper precision, that would be better. Is it possible to perform this operation on the decimal itself without string manipulation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-08-03 02:52:28

要输出 n=3 所需的格式,您可以使用:

number.ToString("0.000###")

使用 n 作为参数,然后您可以构建自定义字符串格式:

string format = "0." + new string('0', n) + new string('#', 6 - n);
s = number.ToString(format);

To output your required format with n=3, you could use:

number.ToString("0.000###")

With n as a parameter, then you could build a custom string format:

string format = "0." + new string('0', n) + new string('#', 6 - n);
s = number.ToString(format);
离去的眼神 2024-08-03 02:52:28

您应该能够通过如下格式设置值来实现此目的:

var str = num.ToString("#0.000#####");

0 的数量决定最小位数,0 的数量加上 #是最大位数。 我不确定您是否真的想要最大值,但我相信这是您能得到的最接近的值。 您当然可以将其设置为任意(大)数字。

You should be able to do this by formatting the value as such:

var str = num.ToString("#0.000#####");

The number of 0s determines the minimum number of digits, and the number of 0s plus #s the maximum number of digits. I'm not sure if you actually want the maximum, but I believe this is the closest you'll get. You could of course just set it to an arbitrary (large) number.

甜妞爱困 2024-08-03 02:52:28

您可以对小数本身执行的任何操作都不够,至少出于以下原因:您无法将您希望在值转换为字符串时打印 x 前导/尾随零的小数本身存储。 .NET中的十进制数据类型只是由128bit组成的值类型,全部专用于表示数字。 不包含有关打印数字时格式的信息,并且它是值类型这一事实允许它作为堆栈上的参数快速传递,这对 GC 来说也是一种缓解。
您可以将小数包装为某些类,在 DAL 中生成该类的实例并返回这些实例的集合(如果您稍后需要在应用程序中进行一些数字运算),如果不是这种情况,您可以简单地在 DAL 中应用“字符串化”并返回一个集合字符串。

Anything you could do on the decimal itself wouldn't be enough for at least the following reason: you couldn't store in a decimal number itself that you want x leading/trailing zeroes to be printed as the value is converted to a string. The decimal data type in .NET is just a value type made up of 128bit, all dedicated to representing the number. No information about formatting when the number is printed is included, and the fact that it's a value type allows it to be quickly passed as an argument on the stack, which is also a relief for the GC.
You could wrap decimal into some class, generate instances of that class in your DAL and return a collection of those if you need to do some number crunching later in the app and if that's not the case you could simply apply the "stringification" in your DAL and return a collection of strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文