Skip to content

Commit

Permalink
Kotlin utvidelser for avrunding av tall (BigDecimal) (#297)
Browse files Browse the repository at this point in the history
* Utvidelser for avrunding av BigDecimal

* Utvidelser for avrunding av BigDecimal

* Utvidelser for avrunding av BigDecimal

* Utvidelser for avrunding av BigDecimal
  • Loading branch information
ugur93 authored Oct 24, 2024
1 parent d0f192c commit 80726d1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ import java.math.MathContext
import java.math.RoundingMode

fun BigDecimalrsbeløpTilMåndesbeløp() = divide(BigDecimal(12), MathContext(10, RoundingMode.HALF_UP))

fun BigDecimal.avrundet(antallDesimaler: Int) = setScale(antallDesimaler, RoundingMode.HALF_UP)

val BigDecimal.avrundetTilNærmesteTier get() = divide(BigDecimal.TEN, 0, RoundingMode.HALF_UP).multiply(BigDecimal.TEN)
val BigDecimal.avrundetMedTiDesimaler get() = avrundet(10)
val BigDecimal.avrundetMedToDesimaler get() = avrundet(2)
val BigDecimal.avrundetMedNullDesimaler get() = avrundet(0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package no.nav.bidrag.domene.util

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import java.math.BigDecimal

class NumberUtilKtTest {
@Test
fun `Skal avrunde tall til desimaler for tall med flere desimaler`() {
val tallSomSkalAvrundes = BigDecimal("10.123456789101112")
tallSomSkalAvrundes.avrundetMedToDesimaler shouldBe BigDecimal("10.12")
tallSomSkalAvrundes.avrundetMedNullDesimaler shouldBe BigDecimal("10")
tallSomSkalAvrundes.avrundetMedTiDesimaler shouldBe BigDecimal("10.1234567891")
}

@Test
fun `Skal avrunde tall til desimaler for tall med få desimaler`() {
val tallSomSkalAvrundes = BigDecimal("10.1")
tallSomSkalAvrundes.avrundetMedToDesimaler shouldBe BigDecimal("10.10")
tallSomSkalAvrundes.avrundetMedNullDesimaler shouldBe BigDecimal("10")
tallSomSkalAvrundes.avrundetMedTiDesimaler shouldBe BigDecimal("10.1000000000")
}

@Test
fun `Skal avrunde tall til nærmeste tier`() {
BigDecimal("1024").avrundetTilNærmesteTier shouldBe BigDecimal("1020")
BigDecimal("1021").avrundetTilNærmesteTier shouldBe BigDecimal("1020")
BigDecimal("1025").avrundetTilNærmesteTier shouldBe BigDecimal("1030")
BigDecimal("1026").avrundetTilNærmesteTier shouldBe BigDecimal("1030")
}
}

0 comments on commit 80726d1

Please sign in to comment.