简体   繁体   English

十进制类型在 .ASP.net Core Razor 绑定中丢失精度

[英]Decimal type loses precision in .ASP.net Core Razor binding

We have been facing issue where .net core rounding decimal values to 2 decimal places we want more precision of upto 5 decimal places.我们一直面临的问题是 .net 核心将十进制值四舍五入到小数点后 2 位,我们希望更精确到小数点后 5 位。 Issue already discussed this question.问题已经讨论过这个问题。 The solution provided doesn't seem to be very elegant.提供的解决方案似乎不是很优雅。 Is there aa way to override this default behavior有没有办法覆盖这个默认行为

1) Use decimal(18, 5) in SQL Server table 1)在SQL Server表中使用decimal(18, 5)

2) Add step="0.00001" in your html input or whatever 2)在您的html输入或其他内容中添加 step="0.00001"

<input type="number" asp-for="Amount" step="0.00001" />

Result结果

在此处输入图片说明

在此处输入图片说明

This is still old.这还是旧的。 It has been around since old .net framework.它从旧的 .net 框架就存在了。 Don't understand why you find it so hard to solve.不明白为什么你觉得很难解决。 oo

您可以将 asp-format 用于输入标签,如下所示:

 <input asp-for='data' asp-format="{0:0.00000}" class="form-control" />

Another option is to write your own tag helper that format the number with 5 decimal places.另一种选择是编写您自己的标签助手,用 5 个小数位格式化数字。 And it can apply through out the application.它可以在整个应用程序中应用。 This way, you have more control on how/where you display the numbers.这样,您可以更好地控制显示数字的方式/位置。 And all formatting code/logic is put in 1 place, so easier for you to change in future.并且所有格式化代码/逻辑都放在 1 个地方,因此您将来更容易更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM