DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Practical Guide to Temporal Workflow Design Patterns
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java
  • Context Rot: Why Your AI Agent Gets Worse the Longer It Works
  • Intelligent Matching and Semantic Search for Marketplace Applications Using OpenAI and .NET

Trending

  • Testing Strategies for Web Development Code Generated by LLMs
  • The Cross-Lingual RAG Problem Nobody Is Talking About
  • AI-Driven RAG Systems: Practical Implementation With LangChain
  • Alternative Structured Concurrency
  1. DZone
  2. Data Engineering
  3. Data
  4. Moving the Decimal Place in a Double

Moving the Decimal Place in a Double

By 
Peter Lawrey user avatar
Peter Lawrey
·
Nov. 25, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
15.4K Views

Join the DZone community and get the full member experience.

Join For Free

 This is taken from a popular answer to the question Moving decimal places over in a double

Question

So I have a double set to equal 1234, I want to move a decimal place over to make it 12.34
So to do this I multiply .1 to 1234 two times, kinda like this
    double x = 1234;
    for(int i = 1; i <= 2; i++)
    {
      x = x*.1;
    }
    System.out.println(x);

This will print the result, "12.340000000000002"
Is there a way, without simply formatting it to two decimal places, to have the double store 12.34 correctly?

Answer

If you use double or float, you should use rounding or expect to see some rounding errors. If you can't do this, use BigDecimal.
The problem you have is that 0.1 is not an exact representation, and by performing the calculation twice, you are compounding that error.
However, 100 can be represented accurately, so try:
double x = 1234;
x /= 100;
System.out.println(x);
which prints:
12.34
This works because Double.toString(d) performs a small amount of rounding on your behalf, but it is not much. If you are wondering what it might look like without rounding:
System.out.println(new BigDecimal(0.1));
System.out.println(new BigDecimal(x));
prints
0.100000000000000005551115123125782702118158340454101562
12.339999999999999857891452847979962825775146484375


In short, rounding is unavoidable for sensible answers in floating point whether you are doing this explicitly or not.

Note: x / 100 and x * 0.01 are not exactly the same when it comes to rounding error. This is because the round error for the first expression depends on the values of x, whereas the 0.01 in the second has a fixed round error.
for(int i = 0; i < 200; i++) {
    double d1 = (double) i / 100;
    double d2 = i * 0.01;
    if (d1 != d2)
        System.out.println(d1 + " != "+d2);
}
prints
0.35 != 0.35000000000000003
0.41 != 0.41000000000000003
0.47 != 0.47000000000000003
0.57 != 0.5700000000000001
0.69 != 0.6900000000000001
0.7 != 0.7000000000000001
0.82 != 0.8200000000000001
0.83 != 0.8300000000000001
0.94 != 0.9400000000000001
0.95 != 0.9500000000000001
1.13 != 1.1300000000000001
1.14 != 1.1400000000000001
1.15 != 1.1500000000000001
1.38 != 1.3800000000000001
1.39 != 1.3900000000000001
1.4 != 1.4000000000000001
1.63 != 1.6300000000000001
1.64 != 1.6400000000000001
1.65 != 1.6500000000000001
1.66 != 1.6600000000000001
1.88 != 1.8800000000000001
1.89 != 1.8900000000000001
1.9 != 1.9000000000000001
1.91 != 1.9100000000000001




Data Types

Published at DZone with permission of Peter Lawrey. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Temporal Workflow Design Patterns
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java
  • Context Rot: Why Your AI Agent Gets Worse the Longer It Works
  • Intelligent Matching and Semantic Search for Marketplace Applications Using OpenAI and .NET

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook