操作数数据类型Float对于Modulo操作员无效

发布于 2025-02-11 18:13:45 字数 1269 浏览 1 评论 0原文

我试图使用LINQ从SQL获取数据,并进行一些操作,例如当该值大于500时,将我的数字舍入到最接近的1000中。我的列costreproductionnew(float,null)和GuildoverRide。

如果建筑物具有价值,则应将其视为否则CostreproductionNew

,我无法执行操作,因为我获得了内部异常'操作数据类型float对Modulo操作员无效'

allBuildings.Sum(b => (b.BuildingOverride.HasValue && b.BuildingOverride > 0)
                                                           ? b.BuildingOverride.Value

: (b.CostReproductionNew.HasValue ? (Math.Round(b.CostReproductionNew.Value) > 500)
                                                          ? (Math.Round(b.CostReproductionNew.Value) % 1000 >= 500
                                                                ? Math.Round(b.CostReproductionNew.Value) + 1000 - Math.Round(b.CostReproductionNew.Value) % 1000
                                                                   : Math.Round(b.CostReproductionNew.Value) - Math.Round(b.CostReproductionNew.Value) % 1000)
                                                                    : 500
                                                                   : 0)

任何人都可以帮助我如何在此处从浮点转换为int?

公式用于在更简单版本中舍入到1000(以获得更好的可读性)

x = x%1000> = 500? X + 1000 -x%1000:x -x%1000;

I was trying to fetch data from Sql using LINQ and do some operation like rounding of my number to nearest 1000 when the value is greater than 500. I have the column costreproductionnew(float,null) and buildingOverride.

if buildingoveride has value it should be considered else costreproductionNew

I am unable to perform operation as I am getting inner exception 'Operand Data type float is invalid for modulo operator'

allBuildings.Sum(b => (b.BuildingOverride.HasValue && b.BuildingOverride > 0)
                                                           ? b.BuildingOverride.Value

: (b.CostReproductionNew.HasValue ? (Math.Round(b.CostReproductionNew.Value) > 500)
                                                          ? (Math.Round(b.CostReproductionNew.Value) % 1000 >= 500
                                                                ? Math.Round(b.CostReproductionNew.Value) + 1000 - Math.Round(b.CostReproductionNew.Value) % 1000
                                                                   : Math.Round(b.CostReproductionNew.Value) - Math.Round(b.CostReproductionNew.Value) % 1000)
                                                                    : 500
                                                                   : 0)

Can anyone help me how to convert from float to int here?

Formula used for rounding to 1000 in simpler version (for better readability)

x = x % 1000 >= 500 ? x + 1000 - x % 1000 : x - x % 1000;

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

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

发布评论

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

评论(1

赠佳期 2025-02-18 18:13:46
b.CostReplacementNew.HasValue (((int)Math.Round(b.ContentCostReproductionNew.Value)) > 500)

通过在帮助我解决这个问题之前添加(int)

b.CostReplacementNew.HasValue (((int)Math.Round(b.ContentCostReproductionNew.Value)) > 500)

by adding (int) before it helped me to resolve this

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