2023-04-03 16:35:41 +00:00
|
|
|
package dev.inmo.micro_utils.common
|
|
|
|
|
2023-08-05 07:49:45 +00:00
|
|
|
import kotlinx.cinterop.*
|
2023-04-03 16:35:41 +00:00
|
|
|
import platform.posix.snprintf
|
|
|
|
import platform.posix.sprintf
|
|
|
|
|
2023-08-05 07:49:45 +00:00
|
|
|
@OptIn(ExperimentalForeignApi::class)
|
2023-04-03 16:35:41 +00:00
|
|
|
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)
|
2023-04-03 16:35:41 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|