Search code examples
javascalabigdecimal

Elapsed time with only 2 digits


So in my scenario i am calculating times between 2 points and want to show this with only 2 decimal points. This is what i have try:

import java.util.concurrent._
val startTmp = System.nanoTime
// Do my stuff...
val endTime = System.nanoTime
val elapsedTimeInSecondsTmp = BigDecimal(TimeUnit.MILLISECONDS.convert(
          endTmp - startTmp, TimeUnit.NANOSECONDS) / 1000.0)

Solution

  • you can use setScale

    BigDecimal(19.1234).setScale(2, BigDecimal.RoundingMode.DOWN)
    => res: scala.math.BigDecimal = 19.12