How do I print a double value without scientific notation using Java?

前端 未结 14 853
一整个雨季
一整个雨季 2020-11-21 11:52

I want to print a double value in Java without exponential form.

double dexp = 12345678;
System.out.println(\"dexp: \"+dexp);

It shows this

相关标签:
14条回答
  • 2020-11-21 12:17

    Java/Kotlin compiler converts any value greater than 9999999 (greater than or equal to 10 million) to scientific notation ie. Epsilion notation.

    Ex: 12345678 is converted to 1.2345678E7

    Use this code to avoid automatic conversion to scientific notation:

    fun setTotalSalesValue(String total) {
            var valueWithoutEpsilon = total.toBigDecimal()
            /* Set the converted value to your android text view using setText() function */
            salesTextView.setText( valueWithoutEpsilon.toPlainString() )
        }
    
    0 讨论(0)
  • 2020-11-21 12:22

    This may be a tangent.... but if you need to put a numerical value as an integer (that is too big to be an integer) into a serializer (JSON, etc.) then you probably want "BigInterger"

    Example:

    value is a string - 7515904334

    We need to represent it as a numerical in a Json message:

    {
        "contact_phone":"800220-3333",
        "servicer_id":7515904334,
        "servicer_name":"SOME CORPORATION"
    }
    

    We can't print it or we'll get this:

    {
        "contact_phone":"800220-3333",
        "servicer_id":"7515904334",
        "servicer_name":"SOME CORPORATION"
    }
    

    Adding the value to the node like this produces the desired outcome:

    BigInteger.valueOf(Long.parseLong(value, 10))
    

    I'm not sure this is really on-topic, but since this question was my top hit when I searched for my solution, I thought I would share here for the benefit of others, lie me, who search poorly. :D

    0 讨论(0)
提交回复
热议问题