MicroUtils/common/src/mingwX64Main/kotlin/fixed.kt

26 lines
648 B
Kotlin
Raw Normal View History

package dev.inmo.micro_utils.common
2023-08-05 07:49:45 +00:00
import kotlinx.cinterop.*
import platform.posix.snprintf
import platform.posix.sprintf
2023-08-05 07:49:45 +00:00
@OptIn(ExperimentalForeignApi::class)
actual fun Float.fixed(signs: Int): Float {
return memScoped {
val buff = allocArray<ByteVar>(Float.SIZE_BYTES * 2)
sprintf(buff, "%.${signs}f", this@fixed)
buff.toKString().toFloat()
}
}
2023-08-05 07:49:45 +00:00
@OptIn(ExperimentalForeignApi::class)
actual fun Double.fixed(signs: Int): Double {
return memScoped {
val buff = allocArray<ByteVar>(Double.SIZE_BYTES * 2)
sprintf(buff, "%.${signs}f", this@fixed)
buff.toKString().toDouble()
}
}