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

  • SEARCH
  • Home
  • 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 3960028
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:48:14+00:00 2026-05-20T02:48:14+00:00

I need to extract the decimal part of a float number, but I get

  • 0

I need to extract the decimal part of a float number, but I get weird results:

float n = 22.65f;
// I want x = 0.65f, but...

x = n % 1; // x = 0.6499996

x = n - Math.floor(n); // x = 0.6499996185302734

x = n - (int)n; // x = 0.6499996

Why does this happen? Why do I get those values instead of 0.65?

  • 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-20T02:48:14+00:00Added an answer on May 20, 2026 at 2:48 am

    float only has a few digit of precision so you should expect to see a round error fairly easily. try double this has more accuracy but still has rounding errors. You have to round any answer you get to have a sane output.

    If this is not desireable you can use BigDecimal which does not have rounding errors, but has its own headaches IMHO.

    EDIT: You may find this interesting. The default Float.toString() uses minimal rounding, but often its not enough.

    System.out.println("With no rounding");
    float n = 22.65f;
    System.out.println("n= "+new BigDecimal(n));
    float expected = 0.65f;
    System.out.println("expected= "+new BigDecimal(expected));
    
    System.out.println("n % 1= "+new BigDecimal(n % 1));
    System.out.println("n - Math.floor(n) = "+new BigDecimal(n - Math.floor(n)));
    System.out.println("n - (int)n= "+new BigDecimal(n - (int)n));
    
    System.out.println("With rounding");
    System.out.printf("n %% 1= %.2f%n", n % 1);
    System.out.printf("n - Math.floor(n) = %.2f%n", n - Math.floor(n));
    System.out.printf("n - (int)n= %.2f%n", n - (int)n);
    

    Prints

    With no rounding
    n= 22.6499996185302734375
    expected= 0.64999997615814208984375
    n % 1= 0.6499996185302734375
    n - Math.floor(n) = 0.6499996185302734375
    n - (int)n= 0.6499996185302734375
    With rounding
    n % 1= 0.65
    n - Math.floor(n) = 0.65
    n - (int)n= 0.65
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to extract number from string like <p id=example>etc etc(5)</p> <p id=example2>etc etc(-5)</p>
I need to extract a randomly generated part of an URL for a Selenium
Need to extract .co.uk urls from a file with lots of entries, some .com
I need to extract a value from a hidden HTML field, have somewhat figured
I need to extract the URL's from the php datas, how can i achieve
I need to extract this text: Line 1 text. Line 2 text. Line 2
I need to extract (XSLT, xpath, xquery... Preferably xpath) the most deeply nested element
I need to extract the arguments from a function Say, Function_1(arg1,agr2,....argn). Is it possible
I need to extract numbers that are longer than 3 digits and do not
I need to extract images from a FB fan page. I'm able to read

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.