操作数数据类型Float对于Modulo操作员无效
我试图使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过在帮助我解决这个问题之前添加(int)
by adding (int) before it helped me to resolve this