This discussion is archived
1 Reply Latest reply: May 8, 2012 10:17 PM by EJP RSS

Wrong conversion valueOf("168981.29").floatValue())

878731 Newbie
Currently Being Moderated
Hi,

I am using valueOf("168981.29").floatValue()) to convert a string into float in my OAF page. But while doing that I am facing wrong conversion for some numbers.

I am listing some of them below

Before Conversion After Conversion
168981.29--------------------------168981.3
228615.29--------------------------228615.3
169086.29--------------------------169086-3


But even 168981.19 and 168981.39 is working fine. Also 100055.29 is working fine. The issue is with some numbers which is ending with .29 .

Please help me to resolve this.

Thanks in advance.

Regards,
Charls
  • 1. Re: Wrong conversion valueOf("168981.29").floatValue())
    EJP Guru
    Currently Being Moderated
    I am using valueOf("168981.29").floatValue()) to convert a string into float
    Why? Why not just use the literal number?
    The issue is with some numbers which is ending with .29 .
    The issue is that floats only have a precise representation of decimal fractions if the fraction is a multiple of a negative power of 2, e.g. .5, .25, .75, .125, .375, .625, .875, ...

    If you have problems with the rounding you are getting you probably shouldn't be using floating-point at all, you should be using a decimal format.

    This is not a Java question so much as a basic computer-science floating-point question. Search for a paper entitled 'What every computer scientist should know about floating-point'.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points