How to convert days into human-readable weeks and days in Scala

1 Answer

0 votes
object Program {

  // Convert total days into a human‑readable "X weeks and Y days" string
  def toReadableWeeksDays(totalDays: Int): String = {
    val weeks = totalDays / 7      // whole weeks
    val days  = totalDays % 7      // leftover days

    s"$weeks week${if (weeks == 1) "" else "s"} and $days day${if (days == 1) "" else "s"}"
  }

  def main(args: Array[String]): Unit = {
    val days = 26
    
    println(toReadableWeeksDays(days))
  }
}




/*
run:

3 weeks and 5 days

*/

 



answered Dec 31, 2025 by avibootz

Related questions

...