# How to use Float.shouldNotBeWithinPercentageOf method of io.kotest.matchers.floats.matchers class

Best Kotest code snippet using io.kotest.matchers.floats.matchers.Float.shouldNotBeWithinPercentageOf

matchers.kt

Source:matchers.kt

`1package io.kotest.matchers.floats2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldBe6import io.kotest.matchers.shouldNot7import io.kotest.matchers.shouldNotBe8fun exactly(d: Float): Matcher<Float> = object : Matcher<Float> {9 override fun test(value: Float) =10 MatcherResult(11 value == d,12 { "\$value is not equal to expected value \$d" },13 { "\$value should not be equal to \$d" })14}15fun lt(x: Float) = beLessThan(x)16fun beLessThan(x: Float) = object : Matcher<Float> {17 override fun test(value: Float) =18 MatcherResult(19 value < x,20 { "\$value should be < \$x" },21 { "\$value should not be < \$x" })22}23fun lte(x: Float) = beLessThanOrEqualTo(x)24fun beLessThanOrEqualTo(x: Float) = object : Matcher<Float> {25 override fun test(value: Float) =26 MatcherResult(27 value <= x,28 { "\$value should be <= \$x" },29 { "\$value should not be <= \$x" })30}31fun gt(x: Float) = beGreaterThan(x)32fun beGreaterThan(x: Float) = object : Matcher<Float> {33 override fun test(value: Float) =34 MatcherResult(35 value > x,36 { "\$value should be > \$x" },37 { "\$value should not be > \$x" })38}39fun gte(x: Float) = beGreaterThanOrEqualTo(x)40fun beGreaterThanOrEqualTo(x: Float) = object : Matcher<Float> {41 override fun test(value: Float) =42 MatcherResult(43 value >= x,44 { "\$value should be >= \$x" },45 { "\$value should not be >= \$x" })46}47infix fun Float.shouldBeLessThan(x: Float) = this shouldBe lt(x)48infix fun Float.shouldNotBeLessThan(x: Float) = this shouldNotBe lt(x)49infix fun Float.shouldBeLessThanOrEqual(x: Float) = this shouldBe lte(x)50infix fun Float.shouldNotBeLessThanOrEqual(x: Float) = this shouldNotBe lte(x)51infix fun Float.shouldBeGreaterThan(x: Float) = this shouldBe gt(x)52infix fun Float.shouldNotBeGreaterThan(x: Float) = this shouldNotBe gt(x)53infix fun Float.shouldBeGreaterThanOrEqual(x: Float) = this shouldBe gte(x)54infix fun Float.shouldNotBeGreaterThanOrEqual(x: Float) = this shouldNotBe gte(x)55infix fun Float.shouldBeExactly(x: Float) = this shouldBe exactly(x)56infix fun Float.shouldNotBeExactly(x: Float) = this shouldNotBe exactly(x)57fun Float.shouldBeZero() = this shouldBeExactly 0f58fun Float.shouldNotBeZero() = this shouldNotBeExactly 0f59/**60 * Verifies that this float is within [percentage]% of [other]61 *62 * 90.0.shouldBeWithinPercentageOf(100.0, 10.0) // Passes63 * 50.0.shouldBeWithinPercentageOf(100.0, 50.0) // Passes64 * 30.0.shouldBeWithinPercentageOf(100.0, 10.0) // Fail65 *66 */67fun Float.shouldBeWithinPercentageOf(other: Float, percentage: Double) {68 require(percentage > 0.0) { "Percentage must be > 0.0" }69 this should beWithinPercentageOf(other, percentage)70}71/**72 * Verifies that this float is NOT within [percentage]% of [other]73 *74 * 90.0.shouldNotBeWithinPercentageOf(100.0, 10.0) // Fail75 * 50.0.shouldNotBeWithinPercentageOf(100.0, 50.0) // Fail76 * 30.0.shouldNotBeWithinPercentageOf(100.0, 10.0) // Passes77 *78 */79fun Float.shouldNotBeWithinPercentageOf(other: Float, percentage: Double) {80 require(percentage > 0.0) { "Percentage must be > 0.0" }81 this shouldNot beWithinPercentageOf(other, percentage)82}83fun beWithinPercentageOf(other: Float, percentage: Double) = object : Matcher<Float> {84 private val tolerance = other.times(percentage / 100)85 private val range = (other - tolerance)..(other + tolerance)86 override fun test(value: Float) = MatcherResult(87 value in range,88 { "\$value should be in \$range" },89 { "\$value should not be in \$range" })90}...`

Float.shouldNotBeWithinPercentageOf

Using AI Code Generation

`1float1.shouldNotBeWithinPercentageOf(float2, 10.0f)2float1.shouldNotBeWithinPercentageOf(float2, 10.0)3float1.shouldBePositive()4float1.shouldBeNegative()5float1.shouldBeZero()6float1.shouldBeOne()7float1.shouldBeNaN()8float1.shouldBeInfinite()9float1.shouldBeFinite()10float1.shouldBeNormal()11float1.shouldBeSubnormal()12float1.shouldBeExactly(1.0f)13float1.shouldBeExactly(1.0f)`

Float.shouldNotBeWithinPercentageOf

Using AI Code Generation

`1import io.kotest.matchers.floats.shouldNotBeWithinPercentageOf2a.shouldNotBeWithinPercentageOf(12.0f, 10.0f)3import io.kotest.matchers.floats.shouldNotBeWithinTolerance4a.shouldNotBeWithinTolerance(12.0f, 2.0f)5import io.kotest.matchers.floats.shouldNotBeZero6a.shouldNotBeZero()7import io.kotest.matchers.floats.shouldNotBeZero8a.shouldNotBeZero()9import io.kotest.matchers.floats.shouldNotBeZero10a.shouldNotBeZero()11import io.kotest.matchers.floats.shouldNotBeZero12a.shouldNotBeZero()13import io.kotest.matchers.floats.shouldNotBeZero14a.shouldNotBeZero()15import io.kotest.matchers.floats.shouldNotBeZero16a.shouldNotBeZero()17import io.kotest.matchers.floats.shouldNotBeZero18a.shouldNotBeZero()`

Float.shouldNotBeWithinPercentageOf

Using AI Code Generation

`1 fun `shouldNotBeWithinPercentageOf method of io.kotest.matchers.floats.matchers class`() {2 a.shouldNotBeWithinPercentageOf(b, 50f)3 }4 fun `shouldNotBeWithinPercentageOf method of io.kotest.matchers.floats.matchers class`() {5 a.shouldNotBeWithinPercentageOf(b, 50f)6 }7 fun `shouldNotBeWithinPercentageOf method of io.kotest.matchers.floats.matchers class`() {8 a.shouldNotBeWithinPercentageOf(b, 50f)9 }10 fun `shouldNotBeWithinPercentageOf method of io.kotest.matchers.floats.matchers class`() {11 a.shouldNotBeWithinPercentageOf(b, 50f)12 }13 fun `shouldNotBeWithinPercentageOf method of io.kotest.matchers.floats.matchers class`() {14 a.shouldNotBeWithinPercentageOf(b, 50f)15 }16 fun `shouldNotBeWithinPercentageOf method of io.kotest.matchers.floats.matchers class`() {17 a.shouldNotBeWithinPercentageOf(b, 50f)18 }`

Float.shouldNotBeWithinPercentageOf

Using AI Code Generation

`1import io.kotest.matchers.floats.shouldNotBeWithinPercentageOf2import io.kotest.core.spec.style.StringSpec3class FloatMatchersTest : StringSpec({4 "float should not be within percentage of another float" {5 10.0f.shouldNotBeWithinPercentageOf(11.0f, 0.1f)6 10.0f.shouldNotBeWithinPercentageOf(11.0f, 0.09f)7 10.0f.shouldNotBeWithinPercentageOf(11.0f, 0.11f)8 }9})10import io.kotest.matchers.floats.shouldNotBeWithinPercentageOf11import io.kotest.core.spec.style.StringSpec12class FloatMatchersTest : StringSpec({13 "float should not be within percentage of another float" {14 10.0f.shouldNotBeWithinPercentageOf(11.0f, 0.1)15 10.0f.shouldNotBeWithinPercentageOf(11.0f, 0.09)16 10.0f.shouldNotBeWithinPercentageOf(11.0f, 0.11)17 }18})19import io.kotest.matchers.floats.shouldNotBeWithinPercentageOf20import io.kotest.core.spec.style.StringSpec21class FloatMatchersTest : StringSpec({22 "float should not be within percentage of another float" {23 10.0f.shouldNotBeWithinPercentageOf(11.0f, 10L)`

## 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.

## LambdaTest Learning Hubs:

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

## Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

## Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!