Decimal Places

Discussion in 'Java' started by Bharat Bhushan, Aug 8, 2003.

  1. Hi,

    How can I store upto 10 decimal places in a variable. I have tried this code
    with float and double datatype but it only prints 1.0

    public class tmp
    {
    static void main(String[] args)
    {
    float a1 = 0.0000000000f;
    a1 = 2/3;
    System.out.print(a1);
    }
    }

    It only prints 0.0 whereas I would like it to print 0.6666666666


    Thanks for your time.

    - Bharat.
     
    Bharat Bhushan, Aug 8, 2003
    #1
    1. Advertising

  2. Bharat Bhushan

    Tim Slattery Guest

    "Bharat Bhushan" <> wrote:

    >Hi,
    >
    >How can I store upto 10 decimal places in a variable. I have tried this code
    >with float and double datatype but it only prints 1.0


    Floats are approximations. Your decimal fraction is converted to
    binary, which yields inexactnesses, then stuffed it into a double or
    float which can lose significant digits.

    If you need precision, especially if you need precision in decimal
    fractions, use BigDecimal instead.

    --
    Tim Slattery
     
    Tim Slattery, Aug 8, 2003
    #2
    1. Advertising

  3. Bharat Bhushan

    Lee Weiner Guest

    In article <cMQYa.6006$>, "Bharat Bhushan" <> wrote:
    >Hi,
    >
    >How can I store upto 10 decimal places in a variable. I have tried this code
    >with float and double datatype but it only prints 1.0
    >
    >public class tmp
    >{
    > static void main(String[] args)
    > {
    > float a1 = 0.0000000000f;
    > a1 = 2/3;
    > System.out.print(a1);
    > }
    >}
    >
    >It only prints 0.0 whereas I would like it to print 0.6666666666


    In this particular case, the problem is your division. When Java divides an
    integer by an integer, it performs integer division and drops the remainder.
    Try: a1 = 2/3.0; and see what happens.

    Lee Weiner
    lee AT leeweiner DOT org
     
    Lee Weiner, Aug 8, 2003
    #3
    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. Eoin Mcloughlin

    Decimal places and huge fractions.

    Eoin Mcloughlin, Feb 11, 2004, in forum: Perl
    Replies:
    5
    Views:
    1,621
    Eoin Mcloughlin
    Feb 13, 2004
  2. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    345
    Curt_C [MVP]
    May 18, 2004
  3. Curt_C [MVP]

    Re: beginner: prob with decimal places

    Curt_C [MVP], May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    549
    Curt_C [MVP]
    May 18, 2004
  4. Harry

    Remove Decimal Places

    Harry, Jun 3, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    8,993
    VB Programmer
    Jun 3, 2004
  5. Replies:
    14
    Views:
    64,847
    rkjessop
    Jul 6, 2012
Loading...

Share This Page