All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.github.lyxnx.util.Number.kt Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
@file:JvmName("Numbers")

package io.github.lyxnx.util

import kotlin.jvm.JvmName
import kotlin.math.round

/**
 * Rounds this double to [dp] decimal places
 */
public fun Double.round(dp: Int): Double {
    var multiplier = 1.0
    repeat(dp) { multiplier *= 10 }
    return round(this * multiplier) / multiplier
}

/**
 * Rounds this float to [dp] decimal places
 */
public fun Float.round(dp: Int): Float {
    var multiplier = 1.0f
    repeat(dp) { multiplier *= 10 }
    return round(this * multiplier) / multiplier
}

/**
 * Tests whether this integer has a specified flag mask. This is used in cases where integers are used as flags and a
 * bitmask is applied to add a flag:
 * * `flags | FLAG_NAME` (Kotlin: `flags or FLAG_NAME`) to add a flag
 * * `flags & ~FLAG_NAME` (Kotlin: `flags and FLAG_NAME.inv()`) to remove a flag
 */
public fun Int.hasFlag(flag: Int): Boolean = (this and flag) == flag




© 2015 - 2024 Weber Informatics LLC | Privacy Policy