This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Need to convert a float value into a normal value

Zubi Pen
Greenhorn
Posts: 26
Hey peeps,

I am a bit stuck here. I am outputing the sum of int values from a database. Because the total sum is very high, it exceeded the maximum value that can fit into an int and hence the output gives me the value 2147483647 which is the maximum value of int. When I cast the sum of the int values into a "float" and output the float value, I get the value 4.067851822E9. I cannot cast the int values into "long" as sequal server doesn't accept long as a datatype. How can I convert the float value into a normal number which in my case should be 4067851822.

Any Ideas?

Thanks,
Ket

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
a number is just that - a number. it doesn't matter if you write it down as 4.067851822E9 or 4067851822 or in roman numerals - they are all the same thing. if you are trying to pass it to something else, it shouldn't matter. if you are trying to print it, you have a display/format issue, not a value issue.

so the question is, what are you really trying to do, and what problem are you having?

Henry Wong
author
Marshal
Posts: 21755
85
When I cast the sum of the int values into a "float" and output the float value, I get the value 4.067851822E9. I cannot cast the int values into "long" as sequal server doesn't accept long as a datatype. How can I convert the float value into a normal number which in my case should be 4067851822.

This question comes up way too often -- and you should do a search of these forum for old topics related to this.

There is *no* formatting in a float value. The 4.067851822E9 comes from the outputting process, and not related to any formatting in the float itself. So, there is no convert of the float to anything here.

Basically, you have two options, you can fix your output code to use a format -- maybe using the printf() method instead of println() method. Or you can convert the float to something that can have a format, like a string object.

[EDIT: Beaten by a minute again ... ]

Henry

Vikas Kapoor
Ranch Hand
Posts: 1374
Zubi,

MS SQL Server does have bigint as datatype.

Zubi Pen
Greenhorn
Posts: 26
Thanks for all the replies.

I am trying to output the value of the total sum of int values in a given column. Let me try and explain, basically when I run a query in MS SQL Server for the total sum of int values, I get the figure 4067856822. Since that value is very high to store as an int, I cast it as a float and when I print out the value as a float, I get the value 4.0678569E9. Now when I cast the float value into a long (by doing (long)FloatValue), I get 4067856896.

Hence I am getting different values, I simply want to get the figure 4067856822 and output that on the screen. But having problems. I am not storing the value nor doing any calculations to it.

I hope I am making sense.

Thanks,
Ket

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15571
46
Note that a float is 32 bits, the same as an int. A float does not have infinite precision - it has a precision of about 6 or 7 decimal digits.

If you need an exact answer like "4067856822" (10 digits), then you will not be able to put this into a float and get the exact right answer out of it. You are getting different values because the float cannot contain the exact sum with 10 digits - you get rounding errors.

So, forget about using float, and just use long (which is a 64-bit integer) to do the calculation. How are you exactly summing those numbers? Are you doing that in Java? Why exactly do you think you cannot use long?

Zubi Pen
Greenhorn
Posts: 26
Hi Young,

You are 100% right.. Initially I did use long but for some strange reason, when I use long, I get the value rounded to 2147483647, which is the maximum int. I don't understand why that happens. Here is the code to my query, may be you can spot something, which I may have missed.

The last line of code gets the value as long. So here is the result I get when I set the values as long and float.
1. summaryData.setTotalCommitted(db.getLong(2)); Setting as long, I get 2147483647
2. summaryData.setTotalCommitted(db.getFloat(2)); Setting as float, I get 4.0678569E9 (which then I cast to long but then get an error in the figure)

Any Ideas or clues.. I am blank?

Ket

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15571
46
Try changing this part of the query:

Sum(CAST(" + tableName + ".GrantRequested AS FLOAT))

to this:

Sum(CAST(" + tableName + ".GrantRequested AS BIGINT))

(and use getLong(...)).

Zubi Pen
Greenhorn
Posts: 26
Hi Young!

Thanks for your help, it worked! However I changed it to GrantRequested AS MONEY instead of BIGINT. Cause when I use BIGINT, I get a server error, may be its cause I am using the old version of sql server.

But thanks for your help.. Appreciate it!

Vikas Kapoor
Ranch Hand
Posts: 1374
Zubi Pen wrote:... may be its cause I am using the old version of sql server.

BIGINT is available since MS SQL 2000. You can refer the same link.