org.pkl.thirdparty.kotlin.time.DurationJvm.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkl-tools Show documentation
Show all versions of pkl-tools Show documentation
Fat Jar containing pkl-cli, pkl-codegen-java, pkl-codegen-kotlin, pkl-config-java, pkl-core, pkl-doc, and their shaded third-party dependencies.
/*
* 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