mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Fixed crashes on large numbers (#12)
Currently just showing empty conversion result when can not convert due to expression result being too big, just like in Calculator by Google. Will add a proper message later.
This commit is contained in:
parent
0a48201595
commit
5cd2b3b205
@ -137,10 +137,17 @@ class MainViewModel @Inject constructor(
|
||||
// Kotlin doesn't have a multi catch
|
||||
val calculatedInput = try {
|
||||
val evaluated = Expressions()
|
||||
// Optimal precision, not too low, not too high. Balanced for performance and UX.
|
||||
.eval(cleanInput)
|
||||
.setScale(_userPrefs.value.digitsPrecision, RoundingMode.HALF_EVEN)
|
||||
.stripTrailingZeros()
|
||||
/**
|
||||
* Too long values crash the UI. If the number is too big, we don't do conversion.
|
||||
* User probably wouldn't need numbers beyond infinity.
|
||||
*/
|
||||
if (evaluated.abs() > BigDecimal.valueOf(Double.MAX_VALUE)) {
|
||||
_calculatedValue.value = null
|
||||
return ""
|
||||
}
|
||||
if (evaluated.compareTo(BigDecimal.ZERO) == 0) BigDecimal.ZERO else evaluated
|
||||
} catch (e: Exception) {
|
||||
// Kotlin doesn't have a multi catch
|
||||
|
Loading…
x
Reference in New Issue
Block a user