Floating point precision

Hello

I have a string in the format 41.821874591 -88.273521.

now I want to save the two float values for which I use

st = new StringTokenizer("41.821874591 -88.273521"," ");

float floatnum = new Float(st.nextToken());

when I print floatnum I get 41.821873 I loose 3 places of precision.

I tried DecimalFormat but it only returns String data.

How can I retain the 3 places of decimal?

[439 byte] By [araoa] at [2007-11-27 9:49:56]
# 1
Use a BigDecimal instead. http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html
hunter9000a at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 2
double?
BigDaddyLoveHandlesa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 3
Use a double instead of a float: it is more precise.
prometheuzza at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 4
BTW, this is an unnecessary use of unboxing:>float floatnum = new Float(st.nextToken()); Why not:float floatnum = Float.parseFloat(st.nextToken()); and similarly for dubs.
BigDaddyLoveHandlesa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 5
When float can store 9 decimal digits why do I have to use Double?
araoa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 6
it is not matter how many decimal it can hold, but how precise...Float is single precision, while double is double precision... to read upon single and double go: http://en.wikipedia.org/wiki/Single_precision http://en.wikipedia.org/wiki/Double_precision
yue42a at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 7
And anyway, unless you have a compelling reason, prefer double over float.
BigDaddyLoveHandlesa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 8
> And anyway, unless you have a compelling reason,> prefer double over float.And if double's still not enough, use a BigDecimal, which gives (pratically) infinite precision, but is more complicated to use.
hunter9000a at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 9

> > And anyway, unless you have a compelling reason,

> > prefer double over float.

>

> And if double's still not enough, use a BigDecimal,

> which gives (pratically) infinite precision, but is

> more complicated to use.

BigDaddy thinks BigDecimal is big-diggity

BigDaddyLoveHandlesa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 10
I have compelling reasons to work with float but if it is impossible to use float in this context then I can use double. My question is can't a number say 11.123456789 be stored as a float if not what is the maximum places of decimal a float can represent?
araoa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 11

> I have compelling reasons to work with float

Now you've got my curiosity up. What are these compelling reasons?

> My question is can't a number say 11.123456789

> be stored as a float if not what is the maximum

> places of decimal a float can represent?

Read the links supplied in reply 6 to answer this question.

Message was edited by:

petes1234

petes1234a at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 12
Ha Ha, I have submitted the data schemas to the client and they are expecting it as float data type. I can only change it, if it is impossible to use float here.
araoa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...
# 13

> when I print floatnum I get 41.821873

When you print it how? The truncation is probably happening when you print, not in the value itself. OTHO there are only 24 bits of precision in a float.

> I have submitted the data schemas to the client

In that case you have made a bad mistake. You should have established the capacity and the feasibility of using that format before getting the client to sign it off.

ejpa at 2007-7-13 0:18:30 > top of Java-index,Java Essentials,Java Programming...