小数点导致 CSV 值中的值添加不正确

发布于 2024-10-15 03:12:26 字数 772 浏览 3 评论 0原文

以下 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 技术交流群。

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

发布评论

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

评论(3

苍景流年 2024-10-22 03:12:26

您显式地将 double 转换为 int,您期望会发生什么? :)

You're explicitly converting the double to int, what do you expect would happen? :)

少女七分熟 2024-10-22 03:12:26

您正在将 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 a double instead of an int 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.

许一世地老天荒 2024-10-22 03:12:26

如果网络服务只接受 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.

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