Library contents

Kotlin kotlin.math.sign

Reduce a change in stock to its direction so a dashboard can choose an up or down arrow.

A simple example

kotlin.math.sign

  1. Start with −4
  2. Do this Read the sign
  3. Result −1
Every negative nonzero input has a negative sign, represented here by −1.
Lesson.kt
fun main() {
    println(kotlin.math.sign(-4.0))
}
Output
-1.0

Syntax

Kotlin syntax
kotlin.math.sign(change)
Run these examples in Kotlin

Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.

kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jar

Show the direction of a stock change

A stocktake compares what the scanner found with what the inventory system expected. The report needs to distinguish a shortage, a surplus and an exact match before deciding whether to investigate.

From the programval change = -12.0
Result-1.000
Lesson.kt

fun main() {
    val change = -12.0

    System.out.printf(java.util.Locale.ROOT, "%.3f%n", (kotlin.math.sign(change)).toDouble())
}
Output
-1.000

Removing twelve items gives a direction of minus one. A positive change gives one.

Advertisements

Display a balanced stock count

A closing stocktake finds exactly as many items as the morning ledger predicts. Check how the calculation represents zero change so the report marks neither a shortage nor a surplus.

From the programval balance = 0.0
Result0.000
Lesson.kt
fun main() {
    val balance = 0.0

    System.out.printf(java.util.Locale.ROOT, "%.3f%n", (kotlin.math.sign(balance)).toDouble())
}
Output
0.000

A difference of zero needs a neutral indicator on the dashboard.

Flag a surplus delivery

A store counts five more parcels than expected. Its dashboard displays only the direction of the discrepancy.

Given5
Worked result1

Using 5 gives 1 for this measurement.

What to watch for

A result of zero needs its own display state for no change.

Documentation for kotlin.math.sign