Fixed-point format for floating-point numbers

Discussion in 'Java' started by Motaz Saad, Nov 3, 2005.

  1. Motaz Saad

    Motaz Saad Guest

    Dear All,
    Please how can I fix the significant places (number of digits after the
    float point) for the floating-point numbers in java?
    Thanks In Advance.
    --
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Motaz Khalid Saad.
    Department Of Computer Science
    Information Technology College
    Islamic University - Gaza - Palestine
    http://www.iugaza.edu.ps
    http://www.geocities.com/motsad
    e-mail: ,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Motaz Saad, Nov 3, 2005
    #1
    1. Advertising

  2. Motaz Saad

    Roedy Green Guest

    On 3 Nov 2005 14:52:52 -0800, "Motaz Saad" <>
    wrote, quoted or indirectly quoted someone who said :

    >Please how can I fix the significant places (number of digits after the
    >float point) for the floating-point numbers in java?
    >Thanks In Advance.


    Reframing your question, how do I convert a double to a String?

    see http://mindprod.com/applets/converter.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 3, 2005
    #2
    1. Advertising

  3. Motaz Saad

    Joan Guest

    "Roedy Green" <>
    wrote in message
    news:...
    > On 3 Nov 2005 14:52:52 -0800, "Motaz Saad"
    > <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>Please how can I fix the significant places (number of digits
    >>after the
    >>float point) for the floating-point numbers in java?
    >>Thanks In Advance.

    >
    > Reframing your question, how do I convert a double to a String?
    >
    > see http://mindprod.com/applets/converter.html
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Java custom programming, consulting and
    > coaching.


    Not to be picky, but on current hardware there is no "float
    point"
    rather it is assumed to be at the left of the mantissa bits.
    Joan, Nov 3, 2005
    #3
  4. Motaz Saad wrote:

    > Please how can I fix the significant places (number of digits after the
    > float point) for the floating-point numbers in java?


    Sounds as if you are after either,
    <http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
    ...or..
    <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>
    Andrew Thompson, Nov 3, 2005
    #4
  5. Motaz Saad

    Tim Slattery Guest

    "Motaz Saad" <> wrote:

    >Dear All,
    >Please how can I fix the significant places (number of digits after the
    >float point) for the floating-point numbers in java?
    >Thanks In Advance.


    You can't, that's why they are called floating point numbers.

    If you need to do fixed point arithmetic, look at the BigDecimal
    class.

    --
    Tim Slattery
    Tim Slattery, Nov 4, 2005
    #5
  6. Motaz Saad

    Tex Guest

    > Please how can I fix the significant places (number of digits after the
    > float point) for the floating-point numbers in java?
    > Thanks In Advance.


    Floating point in any language and on any hardware is not suitable for
    fixing significant digits. This question frequently comes up for science
    & engineering wherein there are rules for the number of significant
    digits you should allow for computations, i.e. how accurate your can
    express your results.

    Floating point does not contain all decimal fractions, e.g. 0.10 does not
    exist in floating point. A close binary representation (< 0.10) will be
    stored.
    It is kinda like s = 1/3 actually stores 0.333333 so 1/3 is not stored. So
    when you enter an arbitrary fractional number, say d = 32.5694, that
    number may well not exist, so only an approximation is stored, and
    similarly for interiem computational results.

    Mostly results are close enough for engineering & gov't work to just
    round and convert to a string for people. When exact control of precision
    is required you need to work in integer arithmetic, i.e. many financial
    calculations are done in 1/100 of a cent, because banks don't want to
    lose or gain pennies every now and then, which would result if they used
    floating point to keep our bank records. Physics frequently requires
    great precision and control of significant results and thus integer
    arithmetic. To a mathematician, floating point doesn't work at all.

    You can get (free) math packages to handle very large numbers of
    significant digits if your numbers cannot be handled by standard
    integers.

    --tex
    Tex, Nov 4, 2005
    #6
  7. Motaz Saad

    Tim Slattery Guest

    "Tex" <> wrote:


    >Floating point does not contain all decimal fractions, e.g. 0.10 does not
    >exist in floating point. A close binary representation (< 0.10) will be
    >stored.


    <NITPICK>
    Yes, but that's not because it's "floating point", it's because it's
    *binary* floating point.
    </NITPICK>

    --
    Tim Slattery
    Tim Slattery, Nov 4, 2005
    #7
  8. Motaz Saad

    Joan Guest

    "Tim Slattery" <> wrote in message
    news:...
    > "Tex" <> wrote:
    >
    >
    >>Floating point does not contain all decimal fractions, e.g.
    >>0.10 does not
    >>exist in floating point. A close binary representation (<
    >>0.10) will be
    >>stored.

    >
    > <NITPICK>
    > Yes, but that's not because it's "floating point", it's because
    > it's
    > *binary* floating point.
    > </NITPICK>
    >

    I think it is interesting that I wrote a JNI program in C that
    returns the
    number of bytes on a MS Windows machine hard drive as a float.
    (And it works.)
    Joan, Nov 5, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,642
    Ben Jones
    Mar 2, 2006
  2. Roger Leigh
    Replies:
    4
    Views:
    464
    Roger Leigh
    Nov 12, 2003
  3. Replies:
    4
    Views:
    1,282
    Default User
    Feb 22, 2006
  4. Satpreet
    Replies:
    1
    Views:
    472
    Michael Mair
    Feb 27, 2006
  5. Satpreet
    Replies:
    0
    Views:
    349
    Satpreet
    Feb 27, 2006
Loading...

Share This Page