Reputation: 7605
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()
}
}
Upvotes: 2
Views: 1123
Reputation: 93649
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())
Upvotes: 4