0

In Android Studio using Java, I'm trying to assign the value 12345678.68d to a double variable. But while running the programme, instead of the value I'm trying to assign, the system is assigning 1.234567868E7 to the variable. Surprisingly, as long as I keep the original value to the "one millionth" position, i.e. 1234567.68d, the correct value is assigned to the variable. The problem occurs as soon as I enter the "ten millionth" digit. Why is this mismatch happening?

I need to pass the actual double value to another fragment/activity where I have to display it accurately using Bundle. So, how can I make sure that a double variable takes the exact double value that I assign to it instead of converting to a weird value?

My code follows. The problem occurs at Line 5, Double dbl = 12345678.68d;

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                Double dbl = 12345678.68d;

                Bundle bundle = new Bundle();
                bundle.putDouble("id", dbl);
                bundle.putString("name", ((EditText) this.getView().findViewById(R.id.etName)).getText().toString());

                getParentFragmentManager().beginTransaction()
                        .setReorderingAllowed(true)
                        .addToBackStack(null)
                        .replace(R.id.fragment_container_view, SecondaryFragment.class, bundle).commit();
                break;
            default:
        }
    }
priyamtheone
  • 517
  • 7
  • 22
  • 5
    `1.234567868E7` and `12345678.68` are exactly the same number. – tkausl Mar 09 '22 at 15:19
  • @tkausl: Then how come they look different? And why Java is making `12345678.68` look like `1.234567868E7`? I need to know more of it to work further. Please explain it a bit. – priyamtheone Mar 09 '22 at 15:26
  • The value has gone into the bundle as an object. Its superficial format is of no interest. Only when displaying might it be – g00se Mar 09 '22 at 15:26
  • @g00se: OK. So how do I convert `1.234567868E7` back to `12345678.68` for displaying in proper order as it was in the beginning? Please explain it a bit further. – priyamtheone Mar 09 '22 at 15:29
  • 1
    `double` **literally** does not distinguish those two values, they are bit-for-bit the exact same value as long as they are stored in a `double`. The *only* place they differ is when you format that `double` as a string. If you care about *how the string looks like* then you need to change how you convert the `double` to a string, for example by using a `DecimalNumberFormat`. – Joachim Sauer Mar 09 '22 at 15:33
  • 1
    String s = String.format("%.2f", d); is a possibility – g00se Mar 09 '22 at 15:34
  • 1
    Note: the number `1.234567868E7` means `1.234567868 * 10⁷` that is `12345678.68` (the decimal point is moved 7 position to the right) – user16320675 Mar 09 '22 at 15:47
  • 1
    The real answer here: you have to step back and **research** how floating point numbers work. And what "scientific notation" is for example. As there are simply **multiple** ways how double values can be REPRESENTED as Strings. – GhostCat Mar 09 '22 at 15:48
  • Also note- it isn't possible to store the exact fraction .68 in a binary value. It will be very close to that value, but it will be slightly rounded to the nearest power of 2 combination. – Gabe Sechan Mar 09 '22 at 15:48
  • https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems – Basil Bourque Mar 09 '22 at 16:28
  • @JoachimSauer: I wrote: `DecimalFormat df = new DecimalFormat("#,###");` `df.setMaximumFractionDigits(340);` `String str = df.format(dbl);` But when I pass **1234567890.0123456789** as the double value, the return value becomes **1,234,567,890.0123458**. Similarly, when I pass **12345678.0123456789**, it returns **12,345,678.01234568**. You can see the fractional precision is lost. But when I pass **12345678.899**, it returns **12,345,678.899**. This time the precision is retained. Why is this happening randomly? How can I make sure the precision remains correct always? A little help, please. – priyamtheone Mar 10 '22 at 18:57

0 Answers0