小数点导致 CSV 值中的值添加不正确
以下 CSV 形式的值 -
事实上,tact
10.5、11.5、15.5、16.5
我有
我将这些值发送到 Web 服务,并将它们存储在数据库中。来自 Web 服务的响应应等于相应列中值的总和 -
即,对于“事实”,返回的响应应等于 10.5 + 15.5,因此 = 26, 对于“tact”11.5 + 16.5,因此= 28。
但是十进制点似乎导致结果响应出现问题,并在获得“fact”= 25和“tact”= 27后忽略数字。
我正在使用以下代码在 csv 中找到适当的值并将其设置为对象数组中的位置 -
upDateFeed[i] = new webservice.upDateFeed();
upDateFeed[i].fact = (int)System.Convert.ToDouble(el.Descendants("var").Where(x => (string)x.Attribute("name") =="fact").SingleOrDefault().Attribute("value").Value);
upDateFeed[i].tact = (int)System.Convert.ToDouble(el.Descendants("var").Where(x => (string)x.Attribute("name") =="tact").SingleOrDefault().Attribute("value").Value);
如何让它考虑十进制点之后的数字?
感谢您抽出时间。
I have the following values in CSV form -
fact, tact
10.5, 11.5
15.5, 16.5
I am sending these to a web service and they are getting stored in a database. The response from the web service should equal the sum of the values in thier respective column -
i.e. for 'fact' the response back should equal 10.5 + 15.5 therefore = 26,
and for 'tact' 11.5 + 16.5 therefore = 28.
however the deciaml point seems to be causing issues with the results response and ignoring the digits after it getting 'fact' = 25 and 'tact' = 27.
I am using the following code to find the appropriate value in the csv and set it to a position in an object array -
upDateFeed[i] = new webservice.upDateFeed();
upDateFeed[i].fact = (int)System.Convert.ToDouble(el.Descendants("var").Where(x => (string)x.Attribute("name") =="fact").SingleOrDefault().Attribute("value").Value);
upDateFeed[i].tact = (int)System.Convert.ToDouble(el.Descendants("var").Where(x => (string)x.Attribute("name") =="tact").SingleOrDefault().Attribute("value").Value);
How can I get it to take the digits after the deciaml points into consideration?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您显式地将
double
转换为int
,您期望会发生什么? :)You're explicitly converting the
double
toint
, what do you expect would happen? :)您正在将 double 转换为 int ,这意味着它将删除任何浮点精度。您需要更新您的
upDateFeed
类,期望使用double
而不是int
,然后您不需要进行任何转换。另外,从可读性的角度来看,您应该稍微分解一下代码,以便更清楚地看到您在做什么。
You are casting your double to an int which means it will remove any floating point precision. You need to update your
upDateFeed
class expect adouble
instead of anint
and then you don't need to do any casting.Also just from a readability point of view you should break up your code a little so it is a bit clearer to see exactly what you are doing.
如果网络服务只接受 int,您将无法获得您想要的数字。它将始终四舍五入
upDateFeed[i].fact
upDateFeed[i].tact
两者都需要是具有小数空格的类型,您对 int 所做的转换意味着小数将被删除。我认为是因为网络服务只接受整数而不接受小数。如果是这种情况,则网络服务需要更改。
if the web service will only accept int's you will not be able to get the figures you want. It will always be rounded
upDateFeed[i].fact
upDateFeed[i].tact
both need to be of a type which has decimal spaces, the cast you are doing to int means the decimals are being removed. I assume because the webservice only accepts ints and not decimals. If that's the case the web service needs to change.