Re: Converting floats to Strings and back



"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


.



Relevant Pages

  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: numpy: handling float(NaN) different in XP vs. Linux
    ... Microsoft Windows XP ... ValueError: invalid literal for float(): NaN ... Convert a string or a number to floating point. ...
    (comp.lang.python)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: inconsistent behavior of >FLOAT
    ... empty string, strings with leading and trailing blanks. ... I think it is too late for>FLOAT to rescind the strong suggestion in its spec that this "should" be the case. ... As long as the spec is tightened to require that a string of blanks must be treated as a valid zero, a programmer would know to filter such a case, if needed, before it is passed to>FLOAT. ... I think ignoring leading and trailing blanks may be reasonable for>FLOAT, as long as it does not violate the current spec. ...
    (comp.lang.forth)