Ebikeneser
Ebikeneser

Reputation: 2364

Decimal Point causing values in CSV values to add incorrectly

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.

Upvotes: 0

Views: 577

Answers (3)

James
James

Reputation: 82096

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.

Upvotes: 1

escargot agile
escargot agile

Reputation: 22389

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

Upvotes: 3

Kev Hunter
Kev Hunter

Reputation: 2625

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.

Upvotes: 0

Related Questions