Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

format float number

Posted on 2009-12-23
32
Medium Priority
?
706 Views
Last Modified: 2013-12-14
Hi Experts

i write a program in C++ and when i assign a value of float,
after i insert the filed to databse i see it like this: -7.8459086983488754E+298

this is the line i use:
*(FLOAT *)(pData + rgBinding[4].obValue) = iRow + 0.5;
iRow is counter of the loop.

i need to insert it to sql server and to see it as regular number.

how do i convert it?

10x
0
Comment
Question by:gudidi
  • 16
  • 8
  • 4
  • +2
30 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 26113482
What is (pData + rgBinding[4].obValue) ?
How is FLOAT defined ?
0
 

Author Comment

by:gudidi
ID: 26113490
//Col5 float
   rgBinding[4].iOrdinal = 5;
   rgBinding[4].wType = DBTYPE_R8;
   rgBinding[4].obStatus = ulOffset;
   ulOffset+=sizeof(DBSTATUS);
   rgBinding[4].obLength = ulOffset;
   ulOffset+=sizeof(DBLENGTH);
   rgBinding[4].obValue = ulOffset;
   ulOffset += sizeof(FLOAT);
   rgBinding[4].cbMaxLen = sizeof(FLOAT);
   rgBinding[4].dwPart = DBPART_VALUE | DBPART_STATUS | DBPART_LENGTH;
   rgBinding[4].eParamIO = DBPARAMIO_NOTPARAM;
   rgBinding[4].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26113723
And the value at (pData + rgBinding[4].obValue) is of type FLOAT ? How is FLOAT defined ?

Does the code that inserts the data in the database understand the FLOAT type, and does it handle it correctly ? How do you insert the data in the database ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:gudidi
ID: 26117573
sorry i did not answered, i had to leave the office.

in database it define as float.
in program also:
rgBinding[4].wType = DBTYPE_R8;
DBTYPE_R8 for OLEDB is float.

maybe i should use currency instead?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26118191
>> DBTYPE_R8 for OLEDB is float.

Not exactly.

If in your C++ code, FLOAT is actually the same as float (a typedef maybe), then that is not likely to be compatible with the floating point format used by your database. Which explains why you get a completely different value in the database.

For setting a floating point value in your database, you need to use the functions provided by the database API. In other words, you need to convert the floating point value used in the C++ code (iRow + 0.5) to a format that is understood by your database BEFORE storing it in the database.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26118194
Why did you accept my reply ? Is the problem solved now ?
0
 

Author Comment

by:gudidi
ID: 26118200
10x for your effort.
the problem did not solve yet, and it looks like the type of the C++ is not like the databse.
any example of convert? i am C# programmer not C++...
0
 

Author Comment

by:gudidi
ID: 26118205
i accept your answer becuae you were kind anough to answer and nobody else did....
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26118207
>> 10x for your effort.

Points are not awarded for effort but for solutions ;) I'll ask to get this question re-opened so we can work on getting you a solution.
0
 

Author Comment

by:gudidi
ID: 26118211
10x
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26118212
>> any example of convert?

You'll need to check the API of your database. What API (and version) are you currently using ?
0
 

Author Comment

by:gudidi
ID: 26118218
i use sql server 2005 , how can i find what do you need?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26118264
That's the version of your database. You mentioned OLEDB before - is that the API you're using ?

I have experience with neither of these, so all I can say is what I find on the internet. For that reason, it's probably better to add some more specific zones to this question, so some experts experienced in that domain can have a look at it.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26118267
>> it's probably better to add some more specific zones to this question, so some experts experienced in that domain can have a look at it.

I'll make the request for you ;)
0
 

Author Comment

by:gudidi
ID: 26118275
10x
0
 

Author Comment

by:gudidi
ID: 26118386
10x a lot
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 26118775
My first thought:
The Db will automatically convert text to the target value.  For instance:
    UPDATE Sales SET nAmt='12345.6789' WHERE nIdx=12345
will convert  the string '12345.6789' into a float value for you as it saves it to the database.  Likewise, if you ask for the value to be converted into a string on the way into your program, then you will have one.
So, although there is probably a more direct method....
You could obtain the data as a string, use a C++ function to convert to a C++ float variable, manipulate it as needed, then convert it back to to a string and save it back to the database as a string.
 Also note: DBTYPE_R8 equates to double, not float:
 http://msdn.microsoft.com/en-us/library/ms711251(VS.85).aspx
0
 

Author Comment

by:gudidi
ID: 26118833
i will try it 10x
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 26122309
What do you mean by "regular number"? Is the value form C different from the one in the database? If so how? Can you give examples with values in C, database and what you expect?

0
 

Author Comment

by:gudidi
ID: 26126508
hi

sorry for the delay (our weekend is in a differnt days...)

when i inser a number of 1.5 for example, i see it in database
like this:
-7.8459086983488754E+298
the problem is not in database because when i update the table manually
i see it correct: 1.5
0
 
LVL 85

Expert Comment

by:ozo
ID: 26126725
I don't see an obvious relationship between 1.5 and -7.8459086983488754E+298
Can you tell us what you get when you store -1, 0, 1, and 2?
Is the value affected when you change what is stored in adjacent fields?
0
 

Author Comment

by:gudidi
ID: 26126730
the same happends with any value.
maybe i need cast for the right side of the statement.

for example if i want to set 1.5, i do i cast it also to float like in the left side?
0
 
LVL 85

Expert Comment

by:ozo
ID: 26126749
If the same thing happens for any value, are you sure you are putting the value in the right place?
0
 

Author Comment

by:gudidi
ID: 26126761
yes.

when you have a float in the left side and you want to cast inside 1.5 int
how to you syntax the casting?
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 26126779
if (pData + rgBinding[4].obValue) really is  a FLOAT*, then you are casting correctly
But I don't think you ever answered wheter your FLOAT was the same as a float

whether or not (pData + rgBinding[4].obValue) really is  a FLOAT *
if the value that you see in the database does not change regardless of that you put there,
it suggest that the value you see in the database may not be coming from there
0
 

Author Comment

by:gudidi
ID: 26126811
well, the table is empty before i make commit.
0
 
LVL 85

Expert Comment

by:ozo
ID: 26126841
If you do not put any value in (pData + rgBinding[4].obValue), do you still get -7.8459086983488754E+298
0
 

Author Comment

by:gudidi
ID: 26126944
good idea i will check it
0
 

Author Comment

by:gudidi
ID: 26126975
you are right.
even when i don't assign any value, i have this strange number inside.
so the problem is in the program when i initialized the array.
0
 

Author Comment

by:gudidi
ID: 26126983
10x a lot.  it is working well.

the type of OLEDB i defined was currency,
i change it to double and it works fine, i see the numbers.
10x for your help.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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