how to convert object to float?

Posted on 2006-05-19
Medium Priority
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
  • 4
  • 2
  • 2
  • +5

Expert Comment

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

Author Comment

ID: 16721114
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.

Expert Comment

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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 16

Expert Comment

ID: 16721273
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

ID: 16721381
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

ID: 16721500
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

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

Expert Comment

ID: 16725189
"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: http://www.skincalc.com/documentation/ search page for "truncate"
Limited stuff on concatenate: http://www.dbazine.com/ofinterest/oi-articles/date1 you can concatenate numbers too!
Some simple stuff on modulo-N: http://www.faqs.org/faqs/computer-lang/Ada/programming/part3/
Close to the actual use of splitting fractions and whole nubmers in float: http://wiki.tcl.tk/879

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

ID: 16729332
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

ID: 16735904
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

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

Author Comment

ID: 16771982
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

GranMod earned 0 total points
ID: 16967851
PAQed with points refunded (20)

Community Support Moderator

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

749 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