我想使用toEngineeringString()
,但它不起作用:
Log.v("smslms",BigDecimal.valueOf(1_000_0000_000_000_000L).toEngineeringString());`
输出:
09-11 16:26:12.221 18492-18492/skarwild.game V/smslms﹕ 10000000000000000
最佳答案
它的行为与 documented 完全相同:
Returns a string that represents the BigDecimal as described in the toString() method, except that if exponential notation is used, the power of ten is adjusted to be a multiple of three (engineering notation) such that the integer part of nonzero values will be in the range 1 through 999.
现在toString()
指定:
A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to a string in base ten using the characters '0' through '9' with no leading zeros (except if its value is zero, in which case a single '0' character is used).
Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, -scale+(ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).
If the scale is greater than or equal to zero and the adjusted exponent is greater than or equal to -6, the number will be converted to a character form without using exponential notation.
在我们的例子中,标度为 0,因此调整后的指数为 16(或类似值)。因此,该值将转换为字符形式,而不使用指数表示法。
关于java - BigDecimal toEngineeringString 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525925/