![]() |
|
[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: ![]() and this is the output: ![]() and this is how its wrote to the DB - ![]() (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 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 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.ExecuteReaderOffcourse 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.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) ![]() Thanks again. |