Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6350763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:52:24+00:00 2026-05-24T21:52:24+00:00

In my JAVA program there is code like this: int f_part = (int) ((f_num

  • 0

In my JAVA program there is code like this:

int f_part = (int) ((f_num - num) * 100);

f_num is double and num is long. I just want to take the fractional part out and assign it to f_part. But some times f_part value is one less than it’s value. Which means if f_num = 123.55 and num = 123, But f_part equals to 54. And it happens only f_num and num is greater than 100. I don’t know why this happening. Please can someone explain why this happens and way to correct it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:52:25+00:00Added an answer on May 24, 2026 at 9:52 pm

    This is due to the limited precision in doubles.

    The root of your problem is that the literal 123.55 actually represents the value 123.54999....

    It may seem like it holds the value 123.55 if you print it:

    System.out.println(123.55);                  // prints 123.55
    

    but in fact, the printed value is an approximation. This can be revealed by creating a BigDecimal out of it, (which provides arbitrary precision) and print the BigDecimal:

    System.out.println(new BigDecimal(123.55));  // prints 123.54999999999999715...
    

    You can solve it by going via Math.round but you would have to know how many decimals the source double actually entails, or you could choose to go through the string representation of the double in fact goes through a fairly intricate algorithm.


    If you’re working with currencies, I strongly suggest you either

    1. Let prices etc be represented by BigDecimal which allows you to store numbers as 0.1 accurately, or
    2. Let an int store the number of cents (as opposed to having a double store the number of dollars).

    Both ways are perfectly acceptable and used in practice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to ask you if there is a possibility to program a Java
Is there any functionality in Java for pausing the program like system(PAUSE); does in
I'd like to port a Linux C program to Java. This program controls a
I want to make a program about Twitter on Android. The code is like
Is there a way to deploy a Java program in a format that is
I know there's some JAVA_OPTS to set to remotely debug a Java program. What
Is there a Generics Friendly way of using Collection.EMPTY_LIST in my Java Program. I
Is there any way, from within a Java program compiled by gcj, to find
I'm making a mini ORM for a Java program I'm writing... there is a
Log.d(TAG, Build.VERSION_CODES.ICE_CREAM_SANDWICH: + Build.VERSION_CODES.ICE_CREAM_SANDWICH); I write code like this, I used the sdk4.0 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.