Last active
January 3, 2022 09:24
-
-
Save ghasemdev/3e2cc7a97b1a7648033122f81855c642 to your computer and use it in GitHub Desktop.
Operator Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.math.pow | |
fun main() { | |
println("^" * 5) // "^^^^^" | |
println(5 `^` 3) // 125.0 | |
println(199 `%%` 24) // (24 * 199) / 100 = 47.76 | |
println(5 / 2) // 2 | |
println(5 `÷` 2) // 2.5 | |
println(1.99999 == 2) // false | |
println(1.99999 `~` 2) // true | |
} | |
infix operator fun String.times(repeat: Int) = repeat(repeat) | |
infix fun <T : Number> T.`^`(other: T) = toDouble().pow(other.toDouble()) | |
infix fun <T : Number> T.`%%`(percentage: T) = (percentage.toDouble() * toDouble()) / 100 | |
infix fun <T : Number> T.`÷`(other: T) = toDouble() / other.toDouble() | |
infix fun <T : Number> T.`~`(other: T) = round(this.toDouble()) == other.toDouble() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment