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

org.pkl.thirdparty.kotlin.time.DurationJvm.kt Maven / Gradle / Ivy

Go to download

Fat Jar containing pkl-cli, pkl-codegen-java, pkl-codegen-kotlin, pkl-config-java, pkl-core, pkl-doc, and their shaded third-party dependencies.

There is a newer version: 0.27.1
Show newest version
/*
 * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.pkl.thirdparty.kotlin.time

import java.math.RoundingMode
import java.text.DecimalFormat
import org.pkl.thirdparty.kotlin.concurrent.getOrSet

internal actual val durationAssertionsEnabled: Boolean = Duration::class.java.desiredAssertionStatus()

private val precisionFormats = Array(4) { ThreadLocal() }

private fun createFormatForDecimals(decimals: Int) = DecimalFormat("0").apply {
    if (decimals > 0) minimumFractionDigits = decimals
    roundingMode = RoundingMode.HALF_UP
}

internal actual fun formatToExactDecimals(value: Double, decimals: Int): String {
    val format = if (decimals < precisionFormats.size) {
        precisionFormats[decimals].getOrSet { createFormatForDecimals(decimals) }
    } else
        createFormatForDecimals(decimals)
    return format.format(value)
}

internal actual fun formatUpToDecimals(value: Double, decimals: Int): String =
    createFormatForDecimals(0)
        .apply { maximumFractionDigits = decimals }
        .format(value)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy