Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Doubles

Hey

I just wondered if I was doing something basic wrong with using doubles. So my program reads values from as database, divides a few together, then writes the new values (always a decimal) to a different column. I have made sure that the column is a double type.

So this is my code: [Image: 675f86d87fe79e9ca1ed1ce39776b679.png]

and this is the output: [Image: ed21ddf05e1efaf793334f2c03a3af80.png]

and this is how its wrote to the DB - [Image: 352b5fa4a663862cc2976af630302412.png]
(ignore the red line, was changing variable names).

Is there something fundemental wrong with the way that I've used the double type?

Cheers
My guess by looking at what you've provided is your GetInt32 method, that method implies that the type it will return will of an a 32bit integer. Therefore, that reader object will type cast your double to an int, and by casting the double you loose precision, hence why your . might be gone
Haze probably nailed it, but I'm still curious can you post your GetInt32 method?
(Sep 4, 2015 03:02 PM)Odus Wrote:  Haze probably nailed it, but I'm still curious can you post your GetInt32 method?

That's just the MySQL data reader method to read from the database. There are other options to read from the database. Such as UInt32 but I wasn't sure of the difference


So what would you suggest? Retrieving it as a string, converting to a double then performing the maths?
(Sep 4, 2015 03:04 PM)SIM-1809171 Wrote:  That's just the MySQL data reader method to read from the database. There are other options to read from the database. Such as UInt32 but I wasn't sure of the difference


So what would you suggest? Retrieving it as a string, converting to a double then performing the maths?

Try using the .ToString() method on your double variable
(Sep 4, 2015 03:04 PM)SIM-1809171 Wrote:  That's just the MySQL data reader method to read from the database. There are other options to read from the database. Such as UInt32 but I wasn't sure of the difference


So what would you suggest? Retrieving it as a string, converting to a double then performing the maths?

I remember when I worked with MSSQL server and VB.NET for my college project, for the MSSQL db connection class there was a way to read all data as objects then convert them as needed. E.g.

Code
Using r As SqlDataReader = SQLCmd.ExecuteReader

      If r.HasRows Then
             While R.Read
             Dim price As Double = Convert.ToDouble(r("cost").ToString())
             End While
      End If

      End Using

Offcourse I freestyled that code but you get the idea. However, since double is a primitive data type, your SQL data reader class should have a method for reading doubles, if it doesnt then read as object and cast.
(Sep 4, 2015 03:20 PM)+Haze Wrote:  I remember when I worked with MSSQL server and VB.NET for my college project, for the MSSQL db connection class there was a way to read all data as objects then convert them as needed. E.g.

Code
Using r As SqlDataReader = SQLCmd.ExecuteReader

      If r.HasRows Then
      While R.Read
      Dim price As Double = Convert.ToDouble(r("cost").ToString())
      End While
      End If

      End Using

Offcourse I freestyled that code but you get the idea. However, since double is a primitive data type, your SQL data reader class should have a method for reading doubles, if it doesnt then read as object and cast.
Thank you haze. As per usual, you hit the nail right on the head with the fact that the GetInt was causing it to change it to an Int. When I used GetDouble (I didnt know that existed, stupid me)

[Image: 45728d087979f311a5a8e4429c755870.png]

Thanks again.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by SoldiercrimesMar 22, 2018 05:27 AM 3 185 Mar 22, 2018 05:27 AM
Last Post: Ninja
[Java] by GpureMay 4, 2013 03:03 PM 11 159 May 4, 2013 03:03 PM
Last Post: vitafortis
[Java] by RoboBlawckMar 28, 2013 04:39 AM 4 127 Mar 28, 2013 04:39 AM
Last Post: vitafortis