String Formatting in Scala

import scala.collection.mutable.StringBuilder

case class Point(x: Int, y: Int)

object StringFormatting {
  def main(args: Array[String]): Unit = {
    // Scala offers several ways to format strings. Here are some examples of
    // common string formatting tasks.

    // To format general Scala values, we can use string interpolation.
    val p = Point(1, 2)
    println(s"struct1: $p")

    // For more complex formatting, we can use the f interpolator.
    println(f"struct2: ${p}%s")

    // To print the type of a value, we can use reflection.
    println(s"type: ${p.getClass}")

    // Formatting booleans is straightforward.
    println(s"bool: ${true}")

    // There are many options for formatting integers.
    // Use %d for standard, base-10 formatting.
    println(f"int: ${123}%d")

    // This prints a binary representation.
    println(s"bin: ${14.toBinaryString}")

    // This prints the character corresponding to the given integer.
    println(s"char: ${33.toChar}")

    // To print hexadecimal, use %x.
    println(f"hex: ${456}%x")

    // There are also several formatting options for floats.
    // For basic decimal formatting use %f.
    println(f"float1: ${78.9}%f")

    // %e and %E format the float in scientific notation.
    println(f"float2: ${123400000.0}%e")
    println(f"float3: ${123400000.0}%E")

    // For basic string printing, we can use string interpolation.
    println(s"str1: ${"\"string\""}")

    // To double-quote strings, we can use raw interpolation.
    println(raw"str2: ${"\"string\""}")

    // To print a representation of a pointer, we can use the hashCode.
    println(s"pointer: ${p.hashCode}")

    // When formatting numbers you will often want to control the width and precision.
    // To specify the width of an integer, use %<width>d.
    println(f"width1: |${12}%6d|${345}%6d|")

    // You can also specify the width of printed floats, along with precision.
    println(f"width2: |${1.2}%6.2f|${3.45}%6.2f|")

    // To left-justify, use the - flag.
    println(f"width3: |${1.2}%-6.2f|${3.45}%-6.2f|")

    // You may also want to control width when formatting strings.
    println(f"width4: |${"foo"}%6s|${"b"}%6s|")

    // To left-justify strings, use the - flag as with numbers.
    println(f"width5: |${"foo"}%-6s|${"b"}%-6s|")

    // So far we've been printing directly. We can also format strings without printing.
    val s = s"sprintf: a ${"string"}"
    println(s)

    // In Scala, we don't have a direct equivalent to Fprintf, but we can use
    // StringBuilder to build strings and then write them to any output stream.
    val sb = new StringBuilder()
    sb.append(s"io: an ${"error"}\n")
    System.err.print(sb.toString)
  }
}

To run this Scala program:

$ scalac StringFormatting.scala
$ scala StringFormatting

This will compile and run the Scala code, producing output similar to the original Go program.

Note that Scala’s string formatting capabilities are somewhat different from Go’s. We’ve used string interpolation and the f interpolator to achieve similar results. For more complex formatting needs, you might want to consider using Java’s Formatter class or a third-party library.