I am making a calculator. When I add too many characters, the display automatically gets this scientific notation.
How do I prevent this?
if ((answer == null)) {
answer = 0.0
} else {
answer = (answer * 10) + digit
}
answerEditText.setText(checkLast2Digits(answer.toDouble().toString()))
}
// removes the .0 from the end of the string
private fun checkLast2Digits(answerText: String): String {
val checkLast2Digits = answerText.takeLast(2)
if (checkLast2Digits.equals(".0")) {
return answerText.toString().take(answerText.toString().length - 2)
} else {
return answerText.toString()
}
}
You can use .toBigDecimal().toPlainString()
to avoid the scientific notation. This also removes the trailing zeros after the decimal place, so you don't need that other function either.
answerEditText.setText(answer.toBigDecimal().toPlainString())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.