aspose file tools*
The moose likes Beginning Java and the fly likes Need to convert a float value into a normal value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need to convert a float value into a normal value" Watch "Need to convert a float value into a normal value" New topic
Author

Need to convert a float value into a normal value

Zubi Pen
Greenhorn

Joined: Oct 24, 2007
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Zubi,

MS SQL Server does have bigint as datatype.
Zubi Pen
Greenhorn

Joined: Oct 24, 2007
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

Joined: Aug 16, 2005
Posts: 14430
    
  23

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Zubi Pen
Greenhorn

Joined: Oct 24, 2007
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?

Thanks for your help,
Ket
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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

Joined: Oct 24, 2007
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

Joined: Aug 16, 2007
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to convert a float value into a normal value