how to convert object to float?

Posted on 2006-05-19
Last Modified: 2007-12-19
how to convert object to float?
returnval = results1[0];
returval is a float variable
returnval = (float)results1[0]; -- does not work
compiler is asking me to cast.
Question by:claghorn
    LVL 8

    Expert Comment

    Well, it kind of depends on what you are converting to a float, what is the original value?

    Author Comment

    the value in the postgres database is 5.68,
    when i convert to double to get the value from the microsoft resultset to my microsoft float variable(because it won't let me cast to float) it rounds it to 5
    but alas, the value in my resultset according to microsoft is 5 too?
    hence I have proven that .net C# is incapable of reading the value 5.68 from a postgres database.
    in my result set the value is 5, this is the problem.
    This I think is unsolvable. I'm willing to try suggestions though.
    LVL 8

    Expert Comment

    I have never used Postgres nor C#, sorry.  Perhaps some other expert will join and add their input.
    LVL 16

    Expert Comment

    Hi claghorn,

    1. Would you show us the code that is ccausing your problem.

    2. Would you like me to move this question to the C# page?

    LVL 25

    Expert Comment

    You cannot cast an object to a primitive directly; convert it to the Float data type, then use the Float class's floatValue() method to return a primitive.

    Author Comment

    select "baladjust" from specialmay where "cls_acct_id" = '0018100925'
    When I run the above statement in C# it returns a value of 5 whereas when I run this statement in Postgres sql window, the value is 5.68
    The double quotes are in the sql as part of the column name. The problem is in the result set itself not casting or converting.
    What do you do when the resultset itself fails to read the value properly?
    Here's my whole function. Yes, please move it to the C# section.
    I've been using double to avoid having to cast the resultset value to float which seemingly can't be done.
    Remember the value "baladjust" in the database is 5.68

    private static double getBalAdjust(System.String cls_acct_id)

            System.String sqlText5;
            double returnval = 1;
            //UPGRADE_TODO: Interface 'java.sql.ResultSet' was converted to 'System.Data.OleDb.OleDbDataReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javasqlResultSet'"
            System.Data.OleDb.OleDbDataReader results1;

            sqlText5 = "Select " + "\"" + "baladjust" + "\"" + " from " + "\"" + "specialmay" + "\"" + " where " + "\"" + "cls_acct_id" + "\"" + " = " + "'" + cls_acct_id + "'";

            System.Console.Out.WriteLine("The Statement looks like this: " + sqlText5 + "\n");

            sql = SupportClass.TransactionManager.manager.CreateStatement(db);
            System.Data.OleDb.OleDbCommand temp_OleDbCommand;
            temp_OleDbCommand = sql;
            temp_OleDbCommand.CommandText = sqlText5;
            results1 = temp_OleDbCommand.ExecuteReader();

            if (results1 != null)
                while (results1.Read())
                    returnval = System.Convert.ToDouble(results1[1 - 1]);
                    //returnval = results1[1 - 1];


            return returnval;
    LVL 32

    Expert Comment

    What is the TYPE of the column "baladjust" in the Postgres schema for this table?
    LVL 12

    Expert Comment

    "hence I have proven that .net C# is incapable of reading the value 5.68 from a postgres database" no you haven't; you've proven though that you don't know how to do it.

    You have a single precision floating point number and a double precision floating point number; you initially cast it as single precision and now you want to work on it as a double precision floating point number.  So, cast it first as a double precision floating point number and you will solve your problem.

    You could also have split it by modulo N, truncate, and concatenate, none of which you employed.  It were simpler to treat floating point as a logical or binary value than a decimal number.  Basically, you take advantage of the fact that floating point is a binary representation of both mantissa and exponenent and thereby perform logical operations rather than mathematical operations.  This is the advantage is efficiency of floating point; you use binary math, which is simple gating logic, to do mathematical operations and at the very end convert to decimal numbers, which requires a software routine usually.  Someone at college didn't tell you the reason you use floating point, it's because it's faster, can be done completely in hardware with logical gating, the results are more accurate, and you don't need a recursive function or algorithim to "fix the resulting number."

    In floating point you operate on the exponents and mantissa's separately, as logical values, independent of one another.  any mathematical operation can be perform in only one clock, including divide and multiply, that is the advantage of floating point.

    Any conversion in-between operations incurs an algorithim and loss of time.  You don't convert to decimal until the values are in a linked list or array, and then you do a linked list lookup, Edit with Float, Vector Edit, or whatever you need to translate the resulting table in Vector or Edit mode in the fastest way possible.

    Make it a double precision floating point value first, operate on it as such, get the results, and then convert them to whatever you want, decimal, integer, or leave them as floating point decimal [not quite the same as "floating point" but close].

    Look up Vector Mode, Edit Mode, and Linked List, and how to use them.  More Fortran oriented, but recently incorporated into most other higherlevel languages.  It's how NASA does it, it's how the banks do it, it's also how you're expected to do it by the programmer that's going to hire you.

    You're also mixing java "script" with C; approach it first from a C perspective.  Remember which is the higher level language and therefore in more control with better definitions and efficiency.

    Truncate, the simple explanation: search page for "truncate"
    Limited stuff on concatenate: you can concatenate numbers too!
    Some simple stuff on modulo-N:
    Close to the actual use of splitting fractions and whole nubmers in float:

    And a lot more.  You're basically limiting yourself to decimal and discounting the benefits of know all the other number bases, like binary.  And floating point is a binary concept and uses binary math.  That means it's logic and can do amazing things in only one clock, very fast.  It works as fast as logic gates do, and only requires even the one clock for storage!  Operating on numbers in decimal form incurs loads of routines and algorithims, all of which are completely unnecessary, except perhaps for the human display.

    Rethink how you're doing the whole thing.

    Author Comment

    The resultset changes a 2 decimal precision numeric value to a decimal and truncates the two decimal places. This is poor in my opinion and I have to convert the numeric value to string in my sql statement to get around this.
    I appreciate your offering and I will read up on these things though. Maybe there is a setting in the resultset that I can change to read the value properly without having to trick the system.    
    LVL 12

    Expert Comment

    Okay, you can cross boundaries if you know how to declare properly, that is, you can declare certain objects to be treated as any combination of numeric/string/userdefined.

    Again, this may incur Edit Mode operations.  Edit Mode, Vector Mode, and quite a few others allow for things such as treating a Binary Coded Decimal [BCD] string as and/or as a number.  And that BCD can be declared either single or double precision floating point BCD.  This allows for direct operations in the mod 10 system [decimal].

    What I am offering is not a trick, it's how the hardware actually works; the software algorithims that do the conversions are the trick.

    There are actually operators that will do 2 decimal precision with rounding at the second digit after the decimal point.  In fact, they can do it at any combinational point of exponent/mantissa/fractional part [in floating point, where you float the point sets the binary point {that point in binary that is the equivalent of the decimal point in decimal}].  That is precisely what floating point does, it moves the binary point; decimal point if you want to consider it as decimal.

    More later, must go for now.

    Expert Comment

    In C#, try using Convert.ToSingle, Convert.ToDouble, or Convert.ToDecimal depending upon how you will use the value later.

    Author Comment

    I converted the value to string at the sql level so that the Microsoft Resultset does not convert my value 2.58 to 2.0  

    Word to the wise: when using .NET with Postgresql, use the following Postgres sql string to keep Microsoft from changing your data on you.

    Select to_char(baladjust,'99999D99')

    Hope this helps someone.
    This thread is closed.

    Accepted Solution

    PAQed with points refunded (20)

    Community Support Moderator

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    fizzArray  challenge 1 28
    hasOne  challenge 59 64
    sumHeights  challenge 17 50
    ClickOnce Install - Shortcut Question 3 41
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now