BigDecimal se convierte en una cadena para notación científica

Uno, la escena

    En el proyecto de desarrollo, el tipo bigdecimal se definirá en términos de la cantidad. Por supuesto, a veces la cantidad debe convertirse en una cadena. Naturalmente, podemos escribir el método amount. ToString () como:

costBudgetEntity.getInitTotalAmount (). toString () // Obtiene la cadena del monto del presupuesto inicial

 

Por supuesto, cuando la cantidad es demasiado pequeña, no hay problema para convertirla en una cadena, pero cuando la cantidad es grande, la cadena convertida está en formato de notación científica, que a menudo no es el formato que queremos.

 

por lo tanto

costBudgetEntity.getInitTotalAmount().toString()//金额为12000000输出的结果为1.2E+7这种的字符串 

Entonces, de acuerdo con esta cadena, es imposible realizar algún procesamiento comercial que desee

 

Dos, resolver

        Después de mirar la API BigDecimal, aprendí que existe un método toPlainString (). El tipo de retorno de este método es String. Devuelve la representación de cadena de este objeto BigDecimal sin ningún exponente.

/**
* Returns a string representation of this {@code BigDecimal}
* without an exponent field.  For values with a positive scale,
* the number of digits to the right of the decimal point is used
* to indicate scale.  For values with a zero or negative scale,
* the resulting string is generated as if the value were
* converted to a numerically equal value with zero scale and as
* if all the trailing zeros of the zero scale value were present
* in the result.
*
* The entire string is prefixed by a minus sign character '-'
* (<tt>'&#92;u002D'</tt>) if the unscaled value is less than
* zero. No sign character is prefixed if the unscaled value is
* zero or positive.
*
* Note that if the result of this method is passed to the
* {@linkplain #BigDecimal(String) string constructor}, only the
* numerical value of this {@code BigDecimal} will necessarily be
* recovered; the representation of the new {@code BigDecimal}
* may have a different scale.  In particular, if this
* {@code BigDecimal} has a negative scale, the string resulting
* from this method will have a scale of zero when processed by
* the string constructor.
*
* (This method behaves analogously to the {@code toString}
* method in 1.4 and earlier releases.)
*
* @return a string representation of this {@code BigDecimal}
* without an exponent field.
* @since 1.5
* @see #toString()
* @see #toEngineeringString()
*/
public String toPlainString() {
    if(scale==0) {
        if(intCompact!=INFLATED) {
            return Long.toString(intCompact);
        } else {
            return intVal.toString();
        }
    }
    if(this.scale<0) { // No decimal point
        if(signum()==0) {
            return "0";
        }
        int tailingZeros = checkScaleNonZero((-(long)scale));
        StringBuilder buf;
        if(intCompact!=INFLATED) {
            buf = new StringBuilder(20+tailingZeros);
            buf.append(intCompact);
        } else {
            String str = intVal.toString();
            buf = new StringBuilder(str.length()+tailingZeros);
            buf.append(str);
        }
        for (int i = 0; i < tailingZeros; i++)
            buf.append('0');
        return buf.toString();
    }
    String str ;
    if(intCompact!=INFLATED) {
        str = Long.toString(Math.abs(intCompact));
    } else {
        str = intVal.abs().toString();
    }
    return getValueString(signum(), str, scale);
}

En este punto, vemos en debug:

costBudgetEntity.getInitTotalAmount().toPlainString() //金额为12000000输出的结果为12000000字符串

Tres, demostración de caso

 

Supongo que te gusta

Origin blog.csdn.net/qq_35340913/article/details/109243688
Recomendado
Clasificación