Link to home
Start Free TrialLog in
Avatar of gudidi
gudidiFlag for Israel

asked on

format float number

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
Avatar of Infinity08
Infinity08
Flag of Belgium image

What is (pData + rgBinding[4].obValue) ?
How is FLOAT defined ?
Avatar of gudidi

ASKER

//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;
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 ?
Avatar of gudidi

ASKER

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?
>> 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.
Why did you accept my reply ? Is the problem solved now ?
Avatar of gudidi

ASKER

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++...
Avatar of gudidi

ASKER

i accept your answer becuae you were kind anough to answer and nobody else did....
>> 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.
Avatar of gudidi

ASKER

10x
>> any example of convert?

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

ASKER

i use sql server 2005 , how can i find what do you need?
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.
>> 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 ;)
Avatar of gudidi

ASKER

10x
Avatar of gudidi

ASKER

10x a lot
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
Avatar of gudidi

ASKER

i will try it 10x
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?

Avatar of gudidi

ASKER

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
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?
Avatar of gudidi

ASKER

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?
If the same thing happens for any value, are you sure you are putting the value in the right place?
Avatar of gudidi

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudidi

ASKER

well, the table is empty before i make commit.
If you do not put any value in (pData + rgBinding[4].obValue), do you still get -7.8459086983488754E+298
Avatar of gudidi

ASKER

good idea i will check it
Avatar of gudidi

ASKER

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.
Avatar of gudidi

ASKER

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.