Skip to content

Commit

Permalink
Limit amounts to two decimal places (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvavra authored Jan 24, 2025
1 parent 8ea699c commit 79d6d48
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions shared/src/commonMain/kotlin/io/stepuplabs/spaydkmp/Spayd.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.stepuplabs.spaydkmp

import com.ionspin.kotlin.bignum.decimal.BigDecimal
import com.ionspin.kotlin.bignum.decimal.RoundingMode
import io.stepuplabs.spaydkmp.common.BankAccount
import io.stepuplabs.spaydkmp.common.BankAccountList
import io.stepuplabs.spaydkmp.common.Key
Expand All @@ -10,7 +11,6 @@ import io.stepuplabs.spaydkmp.common.Validator
import io.stepuplabs.spaydkmp.exception.*
import kotlinx.datetime.LocalDate
import kotlinx.datetime.format
import net.thauvin.erik.urlencoder.UrlEncoderUtil

/*
Class that represents and generates SPAYD
Expand All @@ -20,7 +20,7 @@ class Spayd(
private vararg val parameters: Pair<Key, Any>?
) {
// Convenience constructor that accepts map of parameters
constructor(parameters: Map<Key, Any>): this(
constructor(parameters: Map<Key, Any>) : this(
parameters = parameters.mapNotNull { it.key to it.value }.toTypedArray()
)

Expand All @@ -43,7 +43,7 @@ class Spayd(
constantSymbol: Long? = null,
referenceForSender: String? = null,
url: String? = null,
): this(
) : this(
parameters = arrayOf(
Key.BANK_ACCOUNT to bankAccount,
alternativeBankAccounts?.let { Key.ALTERNATIVE_BANK_ACCOUNTS to it },
Expand Down Expand Up @@ -133,7 +133,9 @@ class Spayd(

val valStr = when (parameter.type) {
LocalDate::class -> (value as LocalDate).format(LocalDate.Formats.ISO_BASIC)
BigDecimal::class -> (value as BigDecimal).toStringExpanded()
BigDecimal::class -> (value as BigDecimal)
.roundToDigitPositionAfterDecimalPoint(2, RoundingMode.ROUND_HALF_CEILING)
.toStringExpanded()
else -> sanitize("$value")
}

Expand Down
26 changes: 26 additions & 0 deletions shared/src/commonTest/kotlin/io/stepuplabs/spaydkmp/SpaydTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,30 @@ internal class SpaydTest {

assertEquals(expected, actual)
}

@Test
fun roundingAmountToTwoDecimalsDown() {
val expected = "SPD*1.0*ACC:CZ7603000000000076327632*AM:20.23"

val spayd = Spayd(
Key.BANK_ACCOUNT to BankAccount("CZ7603000000000076327632"),
Key.AMOUNT to "20.234".toBigDecimal()
)
val actual = spayd.toString()

assertEquals(expected, actual)
}

@Test
fun roundingAmountToTwoDecimalsUp() {
val expected = "SPD*1.0*ACC:CZ7603000000000076327632*AM:20.24"

val spayd = Spayd(
Key.BANK_ACCOUNT to BankAccount("CZ7603000000000076327632"),
Key.AMOUNT to "20.235".toBigDecimal()
)
val actual = spayd.toString()

assertEquals(expected, actual)
}
}

0 comments on commit 79d6d48

Please sign in to comment.