Re: Converting floats to Strings and back
- From: "Alun Harford" <usenet@xxxxxxxxxxxxxxxxx>
- Date: Tue, 22 Nov 2005 18:05:17 -0000
"dcMan" <gregkeesey@xxxxxxxxxx> wrote in message
news:Pqngf.2966$lb.237994@xxxxxxxxxxxxxxxxx
> Hello,
> I'm trying to convert a String to a float, do some arithmetic on it, then
> display the new value on a Swing text field. Then do the reverse. Take a
> decimal numeric value the operator has entered from the GUI, convert it to
a
> float do some arithmetic to remove the decimal, convert it back to a
String
> to save it to a database.
>
> The code works fine until I exceed 8 digits. Beyond 8 digits the float is
> stored in memory in scientific notation (found this out by stepping
through
> using the debugger) so when it is converted to a String I do not get the
> correct data. I get something like 1.234567891E7 instead of 12345678.91
Unless you want speed (ie. the arithmetic is hard and you've analysed the
result of using floating-point to do it), or you want to demonstrate some of
the nasty things that happen with floating point, use BigDecimal.
a) It requires significantly less use of your brain, which generally reduces
the number of bugs.
b) Your users shouldn't have to think about the limitations of floating
point without a very good reason.
Alun Harford
.
- Follow-Ups:
- References:
- Converting floats to Strings and back
- From: dcMan
- Converting floats to Strings and back
- Prev by Date: Re: How can I highlight the current row in a JTextArea?
- Next by Date: IPV6 support
- Previous by thread: Re: Converting floats to Strings and back
- Next by thread: Using BigInteger and BigDecimal [WAS: Converting floats to Strings and back]
- Index(es):
Relevant Pages
|