Skip to content

Commit

Permalink
Merge branch 'fix/enc-separator' into 'master'
Browse files Browse the repository at this point in the history
Consistence encumbrance decimal separator

See merge request fmasa/wfrp-master!195
  • Loading branch information
fmasa committed Feb 20, 2021
2 parents af8c733 + ca810d5 commit b106d46
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Parcelable
import com.fasterxml.jackson.annotation.JsonValue
import cz.frantisekmasa.wfrp_master.core.domain.Stats
import kotlinx.parcelize.Parcelize
import java.text.DecimalFormat

@Parcelize
data class Encumbrance(
Expand All @@ -12,6 +13,7 @@ data class Encumbrance(
) : Parcelable {
companion object {
val Zero: Encumbrance = Encumbrance(0.0)
private val formatter = DecimalFormat("#,##0.###")

fun maximumForCharacter(characteristics: Stats): Encumbrance = Encumbrance(
(characteristics.strengthBonus + characteristics.toughnessBonus).toDouble()
Expand All @@ -26,12 +28,7 @@ data class Encumbrance(
operator fun times(multiplier: Int) = Encumbrance(value * multiplier)
operator fun plus(addend: Encumbrance) = Encumbrance(value + addend.value)

override fun toString(): String = value.formatWithMaximumDecimalPlaces(3)
override fun toString(): String = formatter.format(value)
}

fun Iterable<Encumbrance>.sum(): Encumbrance = fold(Encumbrance.Zero) { a, b -> a + b }

private fun Double.formatWithMaximumDecimalPlaces(decimals: Int) =
"%.${decimals}f".format(this)
.trimEnd('0')
.trimEnd('.')
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ class EncumbranceTest {
assertEquals("0.001", Encumbrance(0.001).toString())
assertEquals("10", Encumbrance(10.000).toString())
assertEquals("12", Encumbrance(12.0).toString())
assertEquals("12,000", Encumbrance(12000.0).toString())
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}

dependencies {
classpath("com.android.tools.build:gradle:7.0.0-alpha05")
classpath("com.android.tools.build:gradle:7.0.0-alpha07")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21")

// Firebase-related dependencies
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Aug 31 09:56:08 CEST 2020
distributionBase=GRADLE_USER_HOME
distributionUrl=https://services.gradle.org/distributions/gradle-6.8-bin.zip
distributionUrl=https://services.gradle.org/distributions/gradle-6.8.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit b106d46

Please sign in to comment.