Skip to content

Commit 79d6d48

Browse files
authored
Limit amounts to two decimal places (#6)
1 parent 8ea699c commit 79d6d48

File tree

2 files changed

+32
-4
lines changed
  • shared/src
    • commonMain/kotlin/io/stepuplabs/spaydkmp
    • commonTest/kotlin/io/stepuplabs/spaydkmp

2 files changed

+32
-4
lines changed

shared/src/commonMain/kotlin/io/stepuplabs/spaydkmp/Spayd.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.stepuplabs.spaydkmp
22

33
import com.ionspin.kotlin.bignum.decimal.BigDecimal
4+
import com.ionspin.kotlin.bignum.decimal.RoundingMode
45
import io.stepuplabs.spaydkmp.common.BankAccount
56
import io.stepuplabs.spaydkmp.common.BankAccountList
67
import io.stepuplabs.spaydkmp.common.Key
@@ -10,7 +11,6 @@ import io.stepuplabs.spaydkmp.common.Validator
1011
import io.stepuplabs.spaydkmp.exception.*
1112
import kotlinx.datetime.LocalDate
1213
import kotlinx.datetime.format
13-
import net.thauvin.erik.urlencoder.UrlEncoderUtil
1414

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

@@ -43,7 +43,7 @@ class Spayd(
4343
constantSymbol: Long? = null,
4444
referenceForSender: String? = null,
4545
url: String? = null,
46-
): this(
46+
) : this(
4747
parameters = arrayOf(
4848
Key.BANK_ACCOUNT to bankAccount,
4949
alternativeBankAccounts?.let { Key.ALTERNATIVE_BANK_ACCOUNTS to it },
@@ -133,7 +133,9 @@ class Spayd(
133133

134134
val valStr = when (parameter.type) {
135135
LocalDate::class -> (value as LocalDate).format(LocalDate.Formats.ISO_BASIC)
136-
BigDecimal::class -> (value as BigDecimal).toStringExpanded()
136+
BigDecimal::class -> (value as BigDecimal)
137+
.roundToDigitPositionAfterDecimalPoint(2, RoundingMode.ROUND_HALF_CEILING)
138+
.toStringExpanded()
137139
else -> sanitize("$value")
138140
}
139141

shared/src/commonTest/kotlin/io/stepuplabs/spaydkmp/SpaydTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,30 @@ internal class SpaydTest {
113113

114114
assertEquals(expected, actual)
115115
}
116+
117+
@Test
118+
fun roundingAmountToTwoDecimalsDown() {
119+
val expected = "SPD*1.0*ACC:CZ7603000000000076327632*AM:20.23"
120+
121+
val spayd = Spayd(
122+
Key.BANK_ACCOUNT to BankAccount("CZ7603000000000076327632"),
123+
Key.AMOUNT to "20.234".toBigDecimal()
124+
)
125+
val actual = spayd.toString()
126+
127+
assertEquals(expected, actual)
128+
}
129+
130+
@Test
131+
fun roundingAmountToTwoDecimalsUp() {
132+
val expected = "SPD*1.0*ACC:CZ7603000000000076327632*AM:20.24"
133+
134+
val spayd = Spayd(
135+
Key.BANK_ACCOUNT to BankAccount("CZ7603000000000076327632"),
136+
Key.AMOUNT to "20.235".toBigDecimal()
137+
)
138+
val actual = spayd.toString()
139+
140+
assertEquals(expected, actual)
141+
}
116142
}

0 commit comments

Comments
 (0)