mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 16:55:26 +02:00
Fixed trailing zeros issue
This commit is contained in:
parent
984ee4439b
commit
6ec1e1dfb0
@ -47,6 +47,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
|||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
|
import java.math.RoundingMode
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
@ -114,7 +115,8 @@ class MainViewModel @Inject constructor(
|
|||||||
fun updateEnableAnalytics(enableAnalytics: Boolean) {
|
fun updateEnableAnalytics(enableAnalytics: Boolean) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
userPrefsRepository.updateEnableAnalytics(enableAnalytics)
|
userPrefsRepository.updateEnableAnalytics(enableAnalytics)
|
||||||
FirebaseAnalytics.getInstance(application).setAnalyticsCollectionEnabled(enableAnalytics)
|
FirebaseAnalytics.getInstance(application)
|
||||||
|
.setAnalyticsCollectionEnabled(enableAnalytics)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,14 +145,24 @@ class MainViewModel @Inject constructor(
|
|||||||
// Converting value using a specified precision
|
// Converting value using a specified precision
|
||||||
val convertedValue: BigDecimal =
|
val convertedValue: BigDecimal =
|
||||||
unitFrom.convert(unitTo, mainUIState.inputValue.toBigDecimal(), precision)
|
unitFrom.convert(unitTo, mainUIState.inputValue.toBigDecimal(), precision)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* There is a very interesting bug when trailing zeros are not stripped when input
|
||||||
|
* consists of ZEROS only (0.00000 as an example). This check is a workaround. If the result
|
||||||
|
* is zero, than we make sure there are no trailing zeros.
|
||||||
|
*/
|
||||||
|
val resultValue =
|
||||||
|
if (convertedValue == BigDecimal.ZERO.setScale(precision, RoundingMode.HALF_EVEN)) {
|
||||||
|
KEY_0
|
||||||
|
} else {
|
||||||
// Setting result value using a specified OutputFormat
|
// Setting result value using a specified OutputFormat
|
||||||
mainUIState = mainUIState.copy(
|
when (outputFormat) {
|
||||||
resultValue = when (outputFormat) {
|
|
||||||
OutputFormat.ALLOW_ENGINEERING -> convertedValue.toString()
|
OutputFormat.ALLOW_ENGINEERING -> convertedValue.toString()
|
||||||
OutputFormat.FORCE_ENGINEERING -> convertedValue.toEngineeringString()
|
OutputFormat.FORCE_ENGINEERING -> convertedValue.toEngineeringString()
|
||||||
else -> convertedValue.toPlainString()
|
else -> convertedValue.toPlainString()
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
|
mainUIState = mainUIState.copy(resultValue = resultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user