Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

What I have tried:

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

Java
@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:
        }
    }
Posted
Updated 9-Mar-22 5:27am

Java is (sluggish but) correct
1.234567868E7 is just a notation for
1.234567868 * 10^7 = 12345678.68
 
Share this answer
 
Comments
priyamtheone 9-Mar-22 12:10pm    
So, Java is converting the double value into a scientific number with an "e" to indicate the power of 10. OK, In understand. But question is, how do I change it to 12345678.68 back in the panel where I need to show it to the user in its original format? Any help on that part, please.
CPallini 9-Mar-22 13:43pm    
Java is converting nothing. That's only a notation. In order to obtain the output you need, have a look at one of the many tutorials on 'double numbers formatting in Java'. See, for instance:
https://www.tutorialspoint.com/format-double-type-in-java
Quote:
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,

Both values are the same.
Scientific notation - Wikipedia[^]
Java Data Types[^]

[update]
Quote:
So, Java is converting the double value into a scientific number with an "e" to indicate the power of 10.

No, a double value is a double, no matter what.
When a double is converted to string or printed, formatting occurs, and if you don't tell which formatting you want, default formatting is chosen by Java.
By default, if Java try this format 12345678.68 but the size of converted value is bigger than a default number of chars, it switch to 1.234567E7 because it can convert the value with less chars.
Quote:
But question is, how do I change it to 12345678.68 back

You need to say how you want to format the value.
https://mkyong.com/java/how-to-format-a-double-in-java/[^]
 
Share this answer
 
v3
Comments
priyamtheone 9-Mar-22 12:10pm    
So, Java is converting the double value into a scientific number with an "e" to indicate the power of 10. OK, In understand. But question is, how do I change it to 12345678.68 back in the panel where I need to show it to the user in its original format? Any help on that part is appreciated.
Patrice T 9-Mar-22 12:53pm    
updated solution.
priyamtheone 10-Mar-22 10:56am    
OK, the conversion part is clear. I understand the flow now. There's just one challenge left. In my case, while assigning a value to the variable, how many digits will be there after the decimal is not fixed. It is a dynamic factor. For example, if I assign 12345678.123 to the variable in Activity1, the same 12345678.123 needs to be shown in Activity2. Again, if 12345678.123456 is assigned in Actvity1, the same needs to be shown in Activity2. So you can see the number of digits after the decimal is dynamic. Now, I can convert the scientific notation back to non-scientific form in Activity2 and show it. But before that, how do I identify how many digits after the decimal did the user assign to the variable in Actvity1? Because, right now I'm in Activity2 and the double value that I got from Activity1 is still in scientific notation. That's the only stumbling block.
Quote:
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?
Firstly there is no weird value, as CPallini and Patrice T note above. But you really need to understand that using floating point values can result in apparently incorrect numbers. Floats and Doubles are stored in a particular format in computers, which allows a wide range of values, but is also inherently inaccurate for some numbers. Make sure that you really need double values rather than long types.

See What Every Computer Scientist Should Know About Floating-Point Arithmetic[^].
 
Share this answer
 
v2
Comments
priyamtheone 9-Mar-22 12:10pm    
So, Java is converting the double value into a scientific number with an "e" to indicate the power of 10. OK, In understand. But question is, how do I change it to 12345678.68 back in the panel where I need to show it to the user in its original format? Any help on that part is appreciated.
Richard MacCutchan 9-Mar-22 12:20pm    
No, Java is not doing anything. What you see is just the default method of displaying large numbers. If you want it displayed in a different format then use a Format string as described at String (Java SE 11 & JDK 11 )[^].
priyamtheone 11-Mar-22 14:24pm    
Now I understand the conversion part. There's just one challenge left. In my case, while assigning a value to the variable, how many digits will be there after the decimal is not fixed. It is a dynamic factor. For example, if I assign 12345678.123 to the variable in Activity1, the same 12345678.123 needs to be shown in Activity2. Again, if 12345678.123456 is assigned in Actvity1, the same needs to be shown in Activity2. So you can see the number of digits after the decimal is dynamic. Now, I can convert the scientific notation back to non-scientific form in Activity2 and show it. But before that, how do I identify how many digits after the decimal did the user assign to the variable in Actvity1? Because, right now I'm in Activity2 and the double value that I got from Activity1 is still in scientific notation. That's the only stumbling block. Any help on this issue, please?
Richard MacCutchan 12-Mar-22 3:53am    
You cannot tell from a floating point value how many digits were originally after the decimal point. If that information is important then you should switch to the BigDecimal (Java Platform SE 7 )[^] type.
priyamtheone 12-Mar-22 9:34am    
That's what I switched to. However, in that case, most programmers suggest not to pass a double or float value to create a BigDecimal, as it may not be able to hold that value precisely. A string should always be used. So I wrote:
String str = "12345678.0123456789";
BigDecimal bd = new BigDecimal(str, MathContext.UNLIMITED);
This time, the value is being held properly with exact scale and precision. Seems like in my scenario, where the fractional part is dynamic, this is how I have to do it. Hold the actual value in a string or BigDecimal, and after performing all math operations, use the held value to check on the number of fractional digits and display the final number accordingly. This is what I could fathom by far. Thank you for pointing out the significant tips.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900