Skip to content

Commit

Permalink
Fix foot precision
Browse files Browse the repository at this point in the history
  • Loading branch information
sadellie committed Jan 31, 2024
1 parent dba9fd7 commit 087ba19
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
package com.sadellie.unitto.core.base

/**
* Current maximum scale that will be used in app. Used in various place in code
* Current maximum scale that will be used in app. Used in various places in code.
*/
const val MAX_PRECISION: Int = 1_000
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package com.sadellie.unitto.data.converter.collections

import com.sadellie.unitto.core.base.R
import com.sadellie.unitto.data.converter.UnitID
import com.sadellie.unitto.data.model.UnitGroup
import com.sadellie.unitto.data.model.unit.AbstractUnit
import com.sadellie.unitto.data.model.unit.NormalUnit
import com.sadellie.unitto.data.converter.UnitID
import java.math.BigDecimal

internal val lengthCollection: List<AbstractUnit> by lazy {
Expand All @@ -37,7 +37,7 @@ internal val lengthCollection: List<AbstractUnit> by lazy {
NormalUnit(UnitID.kilometer, BigDecimal.valueOf(1.0E+21), UnitGroup.LENGTH, R.string.unit_kilometer, R.string.unit_kilometer_short),
NormalUnit(UnitID.nautical_mile, BigDecimal.valueOf(1.852E+21), UnitGroup.LENGTH, R.string.unit_nautical_mile, R.string.unit_nautical_mile_short),
NormalUnit(UnitID.inch, BigDecimal.valueOf(25_400_000_000_000_000), UnitGroup.LENGTH, R.string.unit_inch, R.string.unit_inch_short),
NormalUnit(UnitID.foot, BigDecimal.valueOf(304_800_000_000_002_200), UnitGroup.LENGTH, R.string.unit_foot, R.string.unit_foot_short),
NormalUnit(UnitID.foot, BigDecimal.valueOf(304_800_000_000_000_000), UnitGroup.LENGTH, R.string.unit_foot, R.string.unit_foot_short),
NormalUnit(UnitID.yard, BigDecimal.valueOf(914_400_000_000_006_400), UnitGroup.LENGTH, R.string.unit_yard, R.string.unit_yard_short),
NormalUnit(UnitID.mile, BigDecimal.valueOf(1_609_344_000_000_010_500_000.0), UnitGroup.LENGTH, R.string.unit_mile, R.string.unit_mile_short),
NormalUnit(UnitID.light_year, BigDecimal.valueOf(9.460730472E+33), UnitGroup.LENGTH, R.string.unit_light_year, R.string.unit_light_year_short),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ class AllUnitsTest {
foot.checkWith(inch, "100", "1200")
yard.checkWith(foot, "100", "300")
mile.checkWith(foot, "100", "528000")
light_year.checkWith(foot, "0.0000001", "3103914196.85037")
parsec.checkWith(foot, "0.00000001", "1012361411.25044")
kiloparsec.checkWith(foot, "0.00000000001", "1012361411.25044")
megaparsec.checkWith(foot, "0.00000000000001", "1012361411.25044")
light_year.checkWith(foot, "0.0000001", "3103914196.85039")
parsec.checkWith(foot, "0.00000001", "1012361411.25045")
kiloparsec.checkWith(foot, "0.00000000001", "1012361411.25045")
megaparsec.checkWith(foot, "0.00000000000001", "1012361411.25045")
mercury_equatorial_radius.checkWith(kilometer, "1", "2439.7")
venus_equatorial_radius.checkWith(kilometer, "1", "6051.8")
earth_equatorial_radius.checkWith(kilometer, "1", "6371")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ internal sealed class ConverterResult {
data object Error : ConverterResult()
}

internal fun ConverterResult.Time.format(mContext: Context, formatterSymbols: FormatterSymbols): String {
internal fun ConverterResult.Time.format(
mContext: Context,
formatterSymbols: FormatterSymbols
): String {
val result = mutableListOf<String>()

if (day.isGreaterThan(BigDecimal.ZERO)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,6 @@ internal class ConverterViewModel @Inject constructor(
} catch (e: Exception) {
return@launch
}
val calculated2 = try {
Expression(input2.text.ifEmpty { Token.Digit._0 }).calculate()
} catch (e: ExpressionException.DivideByZero) {
_calculation.update { null }
return@launch
} catch (e: Exception) {
return@launch
}

// Update calculation
_calculation.update { if (input1.text.isExpression()) calculated1 else null }
Expand All @@ -469,6 +461,14 @@ internal class ConverterViewModel @Inject constructor(
if (footInchInput) {
// Converted from second text field too
val inches = unitsRepo.getById(UnitID.inch) as DefaultUnit
val calculated2 = try {
Expression(input2.text.ifEmpty { Token.Digit._0 }).calculate()
} catch (e: ExpressionException.DivideByZero) {
_calculation.update { null }
return@launch
} catch (e: Exception) {
return@launch
}
conversion += inches.convert(unitTo, calculated2)
}
when {
Expand Down

0 comments on commit 087ba19

Please sign in to comment.