Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to get the fractional part of this number without so much precision?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to get the fractional part of this number without so much precision?

    Hey guys,

    I have a number type double and want to split it up into its integer and fractional parts. For example, if I have 3.45 i want a variable that has 3 and another one that has 45.

    I have an idea of how to do it, so I started writing. However, I get the following:

    double num = 3.45;
    		double integerPart = Math.floor(num);
    		System.out.println(integerPart);
    		double fractionalPart = num - integerPart;
    		System.out.println(fractionalPart);

    Output:

    3.0
    0.4500000000000002
    When I subtract 3.45 - 3, I get 0.4500000000000002 instead of just 0.45. How can I get that 0.45? Am I doing something wrong?

    Thank you!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to get the fractional part of this number without so much precision?

    The math is done in binary, it is usually reliable, but it can sometimes be a bit off (lol, perhaps a play on words there). As far as I know, there is not much that can be done about it, other than some intuitive checking on your behalf.

    --- Update ---

    Here is a good article explaining it better than me: Java theory and practice: Where's your point?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to get the fractional part of this number without so much precision?

    Then is there a way that I can extract the fractional part away from that double? My main objective is, if I have an imported double variable with any value, say 3.45, then somehow get a variable with the value 45 in it (without the 0. in front).

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to get the fractional part of this number without so much precision?

    A must read: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Here are some possible solutions:

    1. Don't use float/double at all. Instead, use int/long/BigInteger and manually transpose the decimals. This is generally how it's done in the financial world when dealing with money.
    2. Use BigDecimal. I'm hesitant to suggest this because there are some results of operations in base 10 which can only be expressed exactly in the limit as the number of bits goes to infinity, which obviously isn't acceptable.
    3. Define an arbitrary cutoff point. This is much trickier to do because of the "floating point" part of floating point numbers. There's not really a clear-cut answer for how to determine what the cutoff point is unless you know what range of numbers you are dealing with.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to get the fractional part of this number without so much precision?

    If you want a simple solution for numbers with only a limited number of digits after the dot then you can always use multiplication.
    Say, if you already know that your number will never have more then 2 digits after the floating point then you can do:
    int fractionalPart = ((int) (num * 100)) - integerPart;
    Which should give you the correct result in pretty much every case for 2 digits.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to get the fractional part of this number without so much precision?

    You can also use a DecimalFormat which can produce a String representation of you value to 2 decimal points. Then parse that value back to a double/float.
    Improving the world one idiot at a time!

Similar Threads

  1. Format Precision Exception? What's going on?
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2012, 06:47 PM
  2. Possible Loss of Precision
    By Canadian_Pirate in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2011, 01:41 AM
  3. Possible loss of precision (double/int)
    By haloboy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 8th, 2011, 02:23 AM
  4. set Precision
    By sabir in forum Java Theory & Questions
    Replies: 1
    Last Post: March 4th, 2011, 11:32 AM
  5. [SOLVED] Loss of Precision (Double/Int)
    By Scotty in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 9th, 2010, 01:45 PM