# How to use BigDecimal.round method of io.kotest.matchers.stats.matchers class

Best Kotest code snippet using io.kotest.matchers.stats.matchers.BigDecimal.round

matchers.kt

Source:matchers.kt

`1package io.kotest.matchers.stats2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6import java.math.BigDecimal7import java.math.MathContext8import java.math.RoundingMode9import kotlin.math.sqrt10/**11 * Asserts that mean of the Collection elements equals to [value] with default12 * or specific [precision]. Default precision equals 4 digits after decimal point.13 *14 * Opposite of [shouldNotHaveMean]15 *16 * Example:17 * ```18 *19 * val collection = listOf(1, 1, 3)20 * val firstMean = BigDecimal("1.66667")21 * val secondMean = BigDecimal("1.6667")22 *23 * collection.shouldHaveMean(firstMean, 5) // Assertion passes24 * collection.shouldHaveMean(secondMean) // Assertion passes25 *26 * collection.shouldHaveMean(firstMean) // Assertion fails27 * collection.shouldHaveMean(secondMean, 5) // Assertion fails28 * ```29 *30 * @param value - expected mean value31 * @param precision - precision, by default - 4 digits after decimal point32 *33 */34fun <T : Number> Collection<T>.shouldHaveMean(value: BigDecimal, precision: Int = 4) = this should haveMean<T>(value, precision)35/**36 * Asserts that mean of the Collection elements equals to [value] with default37 * or specific [precision]. Default precision equals 4 digits after decimal point.38 *39 * Opposite of [shouldNotHaveMean]40 *41 * Example:42 * ```43 *44 * val collection = listOf(1, 1, 3)45 * val firstMean = 1.6666746 * val secondMean = 1.666747 *48 * collection.shouldHaveMean(firstMean, 5) // Assertion passes49 * collection.shouldHaveMean(secondMean) // Assertion passes50 *51 * collection.shouldHaveMean(firstMean) // Assertion fails52 * collection.shouldHaveMean(secondMean, 5) // Assertion fails53 * ```54 *55 * @param value - expected mean value56 * @param precision - precision, by default - 4 digits after decimal point57 *58 */59fun <T : Number> Collection<T>.shouldHaveMean(value: Double, precision: Int = 4) = this should haveMean<T>(value, precision)60/**61 * Asserts that mean of the Collection elements doesn't equal to [value] with default62 * or specific [precision]. Default precision equals 4 digits after decimal point.63 *64 * Opposite of [shouldHaveMean]65 *66 * Example:67 * ```68 *69 * val collection = listOf(1, 1, 3)70 * val firstMean = BigDecimal("2.0")71 * val secondMean = BigDecimal("1.6666667")72 *73 * collection.shouldNotHaveMean(firstMean) // Assertion passes74 * collection.shouldNotHaveMean(secondMean, 5) // Assertion passes75 *76 * collection.shouldNotHaveMean(BigDecimal("1.6667")) // Assertion fails77 * collection.shouldNotHaveMean(BigDecimal("1.6667"), 4) // Assertion fails78 * ```79 *80 * @param value - not expected mean value81 * @param precision - precision, by default - 4 digits after decimal point82 *83 */84fun <T : Number> Collection<T>.shouldNotHaveMean(value: BigDecimal, precision: Int = 4) = this shouldNot haveMean<T>(value, precision)85/**86 * Asserts that mean of the Collection elements doesn't equal to [value] with default87 * or specific [precision]. Default precision equals 4 digits after decimal point.88 *89 * Opposite of [shouldHaveMean]90 *91 * Example:92 * ```93 *94 * val collection = listOf(1, 1, 3)95 *96 * collection.shouldNotHaveMean(2.0) // Assertion passes97 * collection.shouldNotHaveMean(1.67, 5) // Assertion passes98 *99 * collection.shouldNotHaveMean(1.6667) // Assertion fails100 * collection.shouldNotHaveMean(1.6667, 4) // Assertion fails101 * ```102 *103 * @param value - not expected mean value104 * @param precision - precision, by default - 4 digits after decimal point105 *106 */107fun <T : Number> Collection<T>.shouldNotHaveMean(value: Double, precision: Int = 4) = this shouldNot haveMean<T>(value, precision)108/**109 * Asserts that variance of the Collection elements equals to [value] with default110 * or specific [precision]. Default precision equals 4 digits after decimal point.111 *112 * Opposite of [shouldNotHaveVariance]113 *114 * Example:115 * ```116 *117 * val collection = listOf(1, 2, 3)118 * val firstVariance = BigDecimal("0.66667")119 * val secondVariance = BigDecimal("0.6667")120 *121 * collection.shouldHaveVariance(firstVariance, 5) // Assertion passes122 * collection.shouldHaveVariance(secondVariance) // Assertion passes123 *124 * collection.shouldHaveVariance(firstVariance) // Assertion fails125 * collection.shouldHaveVariance(secondVariance, 5) // Assertion fails126 * ```127 *128 * @param value - expected variance value129 * @param precision - precision, by default - 4 digits after decimal point130 *131 */132fun <T : Number> Collection<T>.shouldHaveVariance(value: BigDecimal, precision: Int = 4) = this should haveVariance<T>(value, precision)133/**134 * Asserts that variance of the Collection elements equals to [value] with default135 * or specific [precision]. Default precision equals 4 digits after decimal point.136 *137 * Opposite of [shouldNotHaveVariance]138 *139 * Example:140 * ```141 *142 * val collection = listOf(1, 2, 3)143 *144 * collection.shouldHaveVariance(0.66667, 5) // Assertion passes145 * collection.shouldHaveVariance(0.6667) // Assertion passes146 *147 * collection.shouldHaveVariance(0.67) // Assertion fails148 * collection.shouldHaveVariance(0.6667, 5) // Assertion fails149 * ```150 *151 * @param value - expected variance value152 * @param precision - precision, by default - 4 digits after decimal point153 *154 */155fun <T : Number> Collection<T>.shouldHaveVariance(value: Double, precision: Int = 4) = this should haveVariance<T>(value, precision)156/**157 * Asserts that variance of the Collection elements doesn't equal to [value] with default158 * or specific [precision]. Default precision equals 4 digits after decimal point.159 *160 * Opposite of [shouldHaveVariance]161 *162 * Example:163 * ```164 *165 * val collection = listOf(1, 2, 3)166 *167 * collection.shouldNotHaveVariance(BigDecimal("1.01"), 5) // Assertion passes168 * collection.shouldNotHaveVariance(BigDecimal("0.666667")) // Assertion passes169 *170 * collection.shouldNotHaveVariance(BigDecimal("0.6667")) // Assertion fails171 * collection.shouldNotHaveVariance(BigDecimal("0.66667"), 5) // Assertion fails172 * ```173 *174 * @param value - not expected variance value175 * @param precision - precision, by default - 4 digits after decimal point176 *177 */178fun <T : Number> Collection<T>.shouldNotHaveVariance(value: BigDecimal, precision: Int = 4) = this shouldNot haveVariance<T>(value, precision)179/**180 * Asserts that variance of the Collection elements doesn't equal to [value] with default181 * or specific [precision]. Default precision equals 4 digits after decimal point.182 *183 * Opposite of [shouldHaveVariance]184 *185 * Example:186 * ```187 *188 * val collection = listOf(1, 2, 3)189 *190 * collection.shouldNotHaveVariance(1.01, 5) // Assertion passes191 * collection.shouldNotHaveVariance(0.666667) // Assertion passes192 *193 * collection.shouldNotHaveVariance(0.6667) // Assertion fails194 * collection.shouldNotHaveVariance(0.66667, 5) // Assertion fails195 * ```196 *197 * @param value - not expected variance value198 * @param precision - precision, by default - 4 digits after decimal point199 *200 */201fun <T : Number> Collection<T>.shouldNotHaveVariance(value: Double, precision: Int = 4) = this shouldNot haveVariance<T>(value, precision)202/**203 * Asserts that standard deviation of the Collection elements equals to [value] with default204 * or specific [precision]. Default precision equals 4 digits after decimal point.205 *206 * Opposite of [shouldNotHaveStandardDeviation]207 *208 * Example:209 * ```210 *211 * val collection = listOf(1, 2, 3)212 *213 * collection.shouldHaveStandardDeviation(BigDecimal("0.82"), 2) // Assertion passes214 * collection.shouldHaveStandardDeviation(BigDecimal("0.8165")) // Assertion passes215 *216 * collection.shouldHaveStandardDeviation(BigDecimal("0.82")) // Assertion fails217 * collection.shouldHaveStandardDeviation(BigDecimal("0.8165"), 5) // Assertion fails218 * ```219 *220 * @param value - expected standard deviation value221 * @param precision - precision, by default - 4 digits after decimal point222 *223 */224fun <T : Number> Collection<T>.shouldHaveStandardDeviation(value: BigDecimal, precision: Int = 4) = this should haveStandardDeviation<T>(value, precision)225/**226 * Asserts that standard deviation of the Collection elements equals to [value] with default227 * or specific [precision]. Default precision equals 4 digits after decimal point.228 *229 * Opposite of [shouldNotHaveStandardDeviation]230 *231 * Example:232 * ```233 *234 * val collection = listOf(1, 2, 3)235 *236 * collection.shouldHaveStandardDeviation(0.82, 2) // Assertion passes237 * collection.shouldHaveStandardDeviation(0.8165) // Assertion passes238 *239 * collection.shouldHaveStandardDeviation(0.82) // Assertion fails240 * collection.shouldHaveStandardDeviation(0.8165, 5) // Assertion fails241 * ```242 *243 * @param value - expected standard deviation value244 * @param precision - precision, by default - 4 digits after decimal point245 *246 */247fun <T : Number> Collection<T>.shouldHaveStandardDeviation(value: Double, precision: Int = 4) = this should haveStandardDeviation<T>(value, precision)248/**249 * Asserts that standard deviation of the Collection elements doesn't equal to [value] with default250 * or specific [precision]. Default precision equals 4 digits after decimal point.251 *252 * Opposite of [shouldHaveStandardDeviation]253 *254 * Example:255 * ```256 *257 * val collection = listOf(1, 2, 3)258 *259 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.8165"), 5) // Assertion passes260 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.8333")) // Assertion passes261 *262 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.8165")) // Assertion fails263 * collection.shouldNotHaveStandardDeviation(BigDecimal("0.82"), 2) // Assertion fails264 * ```265 *266 * @param value - not expected standard deviation value267 * @param precision - precision, by default - 4 digits after decimal point268 *269 */270fun <T : Number> Collection<T>.shouldNotHaveStandardDeviation(value: BigDecimal, precision: Int = 4) = this shouldNot haveStandardDeviation<T>(value, precision)271/**272 * Asserts that standard deviation of the Collection elements doesn't equal to [value] with default273 * or specific [precision]. Default precision equals 4 digits after decimal point.274 *275 * Opposite of [shouldHaveStandardDeviation]276 *277 * Example:278 * ```279 *280 * val collection = listOf(1, 2, 3)281 *282 * collection.shouldNotHaveStandardDeviation(0.8165, 5) // Assertion passes283 * collection.shouldNotHaveStandardDeviation(0.8333) // Assertion passes284 *285 * collection.shouldNotHaveStandardDeviation(0.8165) // Assertion fails286 * collection.shouldNotHaveStandardDeviation(0.82, 2) // Assertion fails287 * ```288 *289 * @param value - not expected standard deviation value290 * @param precision - precision, by default - 4 digits after decimal point291 *292 */293fun <T : Number> Collection<T>.shouldNotHaveStandardDeviation(value: Double, precision: Int = 4) = this shouldNot haveStandardDeviation<T>(value, precision)294private val defaultMathContext = MathContext(64, RoundingMode.HALF_UP)295private fun BigDecimal.round(precision: Int): BigDecimal = this.setScale(precision, RoundingMode.HALF_UP).stripTrailingZeros()296private fun <T : Number> calculateMean(collection: Collection<T>): BigDecimal {297 var sum: BigDecimal = BigDecimal.ZERO298 for (elem in collection) {299 sum += BigDecimal(elem.toString())300 }301 return sum.divide(BigDecimal(collection.size), defaultMathContext)302}303private fun <T : Number> calculateVariance(collection: Collection<T>): BigDecimal {304 val mean: BigDecimal = calculateMean(collection)305 var sumOfSquaredDifferences: BigDecimal = BigDecimal.ZERO306 for (elem in collection) {307 sumOfSquaredDifferences += (BigDecimal(elem.toString()) - mean).pow(2)308 }309 return sumOfSquaredDifferences.divide(BigDecimal(collection.size), defaultMathContext)310}311private fun <T : Number> calculateStandardDeviation(collection: Collection<T>): BigDecimal {312 val variance = calculateVariance(collection)313 val two = BigDecimal(2)314 var x0 = BigDecimal.ZERO315 var x1 = BigDecimal(sqrt(variance.toDouble()))316 while (x0 != x1) {317 x0 = x1318 x1 = variance.divide(x0, defaultMathContext)319 x1 = x1.add(x0)320 x1 = x1.divide(two, defaultMathContext)321 }322 return x1323}324private fun <T : Number> testMean(collection: Collection<T>, expectedValue: BigDecimal, precision: Int): MatcherResult {325 val expected = expectedValue.stripTrailingZeros()326 val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateMean(collection).round(precision)327 return MatcherResult(328 expected.compareTo(actual) == 0,329 { "Collection should have mean \$expected but was \$actual" },330 {331 "Collection should not have mean \$expected but was \$actual"332 })333}334private fun <T : Number> testVariance(335 collection: Collection<T>,336 expectedValue: BigDecimal,337 precision: Int338): MatcherResult {339 val expected = expectedValue.stripTrailingZeros()340 val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateVariance(collection).round(precision)341 return MatcherResult(342 expected.compareTo(actual) == 0,343 { "Collection should have variance \$expected but was \$actual" },344 {345 "Collection should not have variance \$expected but was \$actual"346 })347}348private fun <T : Number> testStandardDeviation(349 collection: Collection<T>,350 expectedValue: BigDecimal,351 precision: Int352): MatcherResult {353 val expected = expectedValue.stripTrailingZeros()354 val actual = if (collection.isEmpty()) BigDecimal.ZERO else calculateStandardDeviation(collection).round(precision)355 return MatcherResult(356 expected.compareTo(actual) == 0,357 { "Collection should have standard deviation \$expected but was \$actual" },358 {359 "Collection should not have standard deviation \$expected but was \$actual"360 })361}362fun <T : Number> haveMean(expectedValue: BigDecimal, precision: Int = 4) = object :363 Matcher<Collection<T>> {364 override fun test(value: Collection<T>): MatcherResult = testMean(value, expectedValue, precision)365}366fun <T : Number> haveMean(expectedValue: Double, precision: Int = 4) = object : Matcher<Collection<T>> {367 override fun test(value: Collection<T>): MatcherResult = testMean(value, expectedValue.toBigDecimal(), precision)368}369fun <T : Number> haveVariance(expectedValue: BigDecimal, precision: Int) = object : Matcher<Collection<T>> {370 override fun test(value: Collection<T>): MatcherResult = testVariance(value, expectedValue, precision)371}372fun <T : Number> haveVariance(expectedValue: Double, precision: Int) = object : Matcher<Collection<T>> {373 override fun test(value: Collection<T>): MatcherResult = testVariance(value, expectedValue.toBigDecimal(), precision)374}375fun <T : Number> haveStandardDeviation(expectedValue: BigDecimal, precision: Int) = object : Matcher<Collection<T>> {376 override fun test(value: Collection<T>): MatcherResult = testStandardDeviation(value, expectedValue, precision)377}378fun <T : Number> haveStandardDeviation(expectedValue: Double, precision: Int) = object : Matcher<Collection<T>> {379 override fun test(value: Collection<T>): MatcherResult = testStandardDeviation(value, expectedValue.toBigDecimal(), precision)380}...`

BigDecimal.round

Using AI Code Generation

`1 import io.kotest.matchers.shouldBe2 import io.kotest.matchers.shouldNotBe3 import io.kotest.matchers.stats.matchers.round4 import java.math.BigDecimal5 fun main() {6 val value = BigDecimal("1.2345")7 value shouldBe round(2)8 value shouldNotBe round(3)9 }`

BigDecimal.round

Using AI Code Generation

`1import io.kotest.matchers.shouldBe2import io.kotest.matchers.stats.matchers.*3import java.math.BigDecimal4import java.math.RoundingMode5fun main() {6val bigDecimal = BigDecimal("1.23456")7val rounded = bigDecimal.round(2, RoundingMode.HALF_EVEN)8rounded shouldBe BigDecimal("1.23")9}10import io.kotest.matchers.shouldBe11import io.kotest.matchers.stats.matchers.*12import java.math.BigDecimal13import java.math.RoundingMode14fun main() {15val bigDecimal = BigDecimal("1.23456")16val rounded = bigDecimal.round(2, RoundingMode.HALF_EVEN)17rounded shouldBe BigDecimal("1.23")18}19import io.kotest.matchers.shouldBe20import io.kotest.matchers.stats.matchers.*21import java.math.BigDecimal22import java.math.RoundingMode23fun main() {24val bigDecimal = BigDecimal("1.23456")25val rounded = bigDecimal.round(2, RoundingMode.HALF_EVEN)26rounded shouldBe BigDecimal("1.23")27}28import io.kotest.matchers.shouldBe29import io.kotest.matchers.stats.matchers.*30import java.math.BigDecimal31import java.math.RoundingMode32fun main() {33val bigDecimal = BigDecimal("1.23456")34val rounded = bigDecimal.round(2, RoundingMode.HALF_EVEN)35rounded shouldBe BigDecimal("1.23")36}37import io.kotest.matchers.shouldBe38import io.kotest.matchers.stats.matchers.*39import java.math.BigDecimal40import java.math.RoundingMode41fun main() {42val bigDecimal = BigDecimal("1.23456")43val rounded = bigDecimal.round(2, RoundingMode.HALF_EVEN)44rounded shouldBe BigDecimal("1.23")45}46import io.kotest.matchers.shouldBe47import io.kotest.matchers.stats.matchers.*48import java.math.BigDecimal49import java`

BigDecimal.round

Using AI Code Generation

`1import java.math.BigDecimal2fun main() {3val rounded = BigDecimal("123.456").round(2)4println(rounded)5}6import java.math.BigDecimal7fun main() {8val rounded = BigDecimal("123.456").setScale(2, BigDecimal.ROUND_HALF_UP)9println(rounded)10}11import java.math.BigDecimal12fun main() {13val str = BigDecimal("123.456").toString()14println(str)15}16import java.math.BigDecimal17fun main() {18val str = BigDecimal("123.456").toPlainString()19println(str)20}21import java.math.BigDecimal22fun main() {23val str = BigDecimal("123.456").toEngineeringString()24println(str)25}26import java.math.BigDecimal27fun main() {28val bigInt = BigDecimal("123.456").toBigInteger()29println(bigInt)30}`

BigDecimal.round

Using AI Code Generation

`1 val roundedValue = value.round(3)2}3@JvmName("roundBigDecimal")4fun BigDecimal.round(scale: Int): BigDecimal = this.setScale(scale, RoundingMode.HALF_EVEN)5@JvmName("roundDouble")6fun Double.round(scale: Int): Double = BigDecimal(this).round(scale).toDouble()7@JvmName("roundFloat")8fun Float.round(scale: Int): Float = BigDecimal(this.toDouble()).round(scale).toFloat()9@JvmName("roundLong")10fun Long.round(scale: Int): Long = BigDecimal(this.toDouble()).round(scale).toLong()11@JvmName("roundInt")12fun Int.round(scale: Int): Int = BigDecimal(this.toDouble()).round(scale).toInt()13@JvmName("roundShort")14fun Short.round(scale: Int): Short = BigDecimal(this.toDouble()).round(scale).toShort()15@JvmName("roundByte")16fun Byte.round(scale: Int): Byte = BigDecimal(this.toDouble()).round(scale).toByte()17@JvmName("roundBigInteger")18fun BigInteger.round(scale: Int): BigInteger = BigDecimal(this.toDouble()).round(scale).toBigInteger()19@JvmName("roundChar")20fun Char.round(scale: Int): Char = BigDecimal(this.toDouble()).round(scale).toChar()21@JvmName("roundString")22fun String.round(scale: Int): String = BigDecimal(this.toDouble()).round(scale).toString()23@JvmName("roundNumber")24fun Number.round(scale: Int): Number = BigDecimal(this.toDouble()).round(scale)25@JvmName("roundAny")26fun Any.round(scale: Int): Any = BigDecimal(this.toString().toDouble()).round(scale)27@JvmName("roundArray")28fun <T : Number> Array<T>.round(scale: Int): Array<T> = this.map { it.round(scale) as T }.toTypedArray()29@JvmName("roundIterable")30fun <T : Number> Iterable<T>.round(scale: Int): Iterable<T> = this.map { it.round(scale) as T }31@JvmName("roundSequence")32fun <T : Number> Sequence<T>.round(scale: Int): Sequence<T> = this.map { it.round(scale) as T }33@JvmName("roundList")34fun <T : Number> List<T>.round(scale: Int): List<T> = this.map { it.round(scale) as T }`

## Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.