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

The Archive Base Latest Questions

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

So I have a double set to equal 1234, I want to move a

  • 0

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?

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

    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
    

    NOTE: This has nothing to do with randomness in your system (or your power supply). This is due to a representation error, which will produce the same outcome every time. The precision of double is limited and in base 2 rather than base 10, so numbers which can be precisely represented in decimal often cann’t be precisely represented in base 2.

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

Sidebar

Related Questions

i have a double, the decimal place isn't fix (8-?) i want to fix
I have two classes set up as follows: class Point { protected: double coords[3];
I have a set of three double values as part of an anonymous expression
Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give
I'm looking to slice a two dimensional array in C#. I have double[2,2] prices
I have a double typed variable. This variable stores information that is part of
In My Application there is two EditText and one TextView. I have set the
I have an UIViewController class with two labels and a UIImageView set as IBOutlets,
I have double values which I would like to convert to String values with
Let’s say I have double length that can be either a real length or

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.