Hack Forums
[C#] Doubles - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Doubles (/showthread.php?tid=4975521)



Doubles - SIM-1809171 - Sep 4, 2015

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


* - +Haze - Sep 4, 2015

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


* - Odus - Sep 4, 2015

Haze probably nailed it, but I'm still curious can you post your GetInt32 method?


* - SIM-1809171 - Sep 4, 2015

(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?


* - Serux - Sep 4, 2015

(Sep 4, 2015 03:04 PM)e4stwood 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


* - +Haze - Sep 4, 2015

(Sep 4, 2015 03:04 PM)e4stwood 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.


* - SIM-1809171 - Sep 4, 2015

(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.