https://kotlinlang.org logo
#announcements
Title
# announcements
d

Daniel

02/29/2020, 8:09 PM
Might there be a better way to multiply an integer with a float and getting a result of Integer? (paddingTop is Int, factor is Float)
Copy code
paddingTop = (paddingTop * factor).toInt()
Maybe an extension function but this seems odd:
Copy code
private fun Int.fMul(factor: Float) = (this * factor).toInt()
a

Andreas Jost

03/03/2020, 9:40 AM
I've been thinking about it. It's just a minor modification from your example, but maybe make your extension function an infix function? So using the function looks more like using an operator:
Copy code
fun main() {
    print(2 fMul 3.2f)
}

private infix fun Int.fMul(other: Float) = (this * other).toInt()
2 Views