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 8774765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:39:00+00:00 2026-06-13T18:39:00+00:00

I’m doing a lot of floating point operations in my application, and I’m aware

  • 0

I’m doing a lot of floating point operations in my application, and I’m aware that floating point operations are not 100% accurate which is fine but when it comes to printing the result I have yet to find a way to format it properly.

for example

0.1 + 0.1 = 0.19999999999999999999999

Math.sin(Math.PI) = 1.2246E16 instead of 0

and stuff like 1.000000000000000001

I’m looking for a general way to take those results and get the proper rounded value (in a String form)

so that
0.1 + 0.1 = 0.19999999999999999999999 -> 0.2 this one requires rounding up

Math.sin(Math.PI) = 1.2246E16 -> 0

and stuff like 1.000000000000000001 -> 1 ; while this one requires rounding down

I’d also like to avoid losing data in cases like when the result is
1.1234567891234567 or 1.33333333333333 in those cases the result should remain the same.

  • 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-06-13T18:39:01+00:00Added an answer on June 13, 2026 at 6:39 pm

    If you want to avoid using formatting, you can round the result to a fixed precision before printing it and use the fact that Java will do a small amount of rounding to hide representation error.

    e.g. To 6 decimal places

    public static double round6(double d) {
        return Math.round(d * 1e6) / 1e6;
    }
    

    The result of round() and 1e6 can be represented exactly. Additionally “IEEE 754 requires correct rounding: that is, the rounded result is as if infinitely precise arithmetic was used to compute the value and then rounded” The only error left is the representation error as it takes the nearest representable value. Java’s toString() assumes that when you have a double which has a nearest representable value of a shorter decimal, that is what you want to see.

    e.g. The actual value for 0.1 is

    System.out.println(new BigDecimal(0.1));
    

    prints

    0.1000000000000000055511151231257827021181583404541015625
    

    but when Double.toString() is passed this value, it determines that 0.1 would be converted to this double and so this is what it should be displayed.

    BTW In Java 6 there is a bug where it doesn’t use the minimum number of digits.

    for (int i = 1; i <= 9; i++)
        System.out.print(i / 1000.0 + " ");
    

    in Java 6 prints

    0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090
    

    and in Java 7 prints

    0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009
    

    I often use printf if performance is not critical and if it is, I use a custom routine to write the double to a direct ByteBuffer to a fixed precision (effectively rounding it) This is faster as it doesn’t create any objects.

    I’d also like to avoid losing data in cases like when the result is 1.1234567891234567 or 1.33333333333333 in those cases the result should remain the same.

    In that case, don’t round the results until you need to display them.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.