Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,624 questions

56,911 answers

573 users

How to format a string using fixed-width fields in Scala

6 Answers

0 votes
/*
Common format specifiers
-----------------------------------
%-10s // Left-aligned string, width 10
%10s // Right-aligned string, width 10
%5d // Integer, width 5
%08d // Integer, width 8, padded with zeros
%12.2f // Floating-point, width 12, 2 decimal places
*/

object FixedWidthFields {

  /**
   * Formats a record using fixed-width fields.
   *
   * %-15s = left-aligned string, width 15
   * %10d  = right-aligned integer, width 10
   * %12.2f = right-aligned decimal number, width 12,
   *           with 2 digits after the decimal point
   */
  def formatRecord(name: String, age: Int, salary: Double): String = {
    f"$name%-15s$age%10d$salary%12.2f"
  }

  def main(args: Array[String]): Unit = {

    // Store the formatted string in a variable
    val result = formatRecord("Alice", 26, 125000.0)

    println(result)
  }
}


/*
run:

Alice                  26   125000.00

*/

 



answered 3 days ago by avibootz
edited 3 days ago by avibootz
0 votes
object SimpleStringExample {

  def main(args: Array[String]): Unit = {

    // Left-align "Hello" in a field 10 characters wide
    val result = f"${"Hello"}%-10s"

    println(s"|$result|")
  }
}


/*
run:

|Hello     |

*/

 



answered 3 days ago by avibootz
0 votes
// Right-aligned field

object RightAlignExample {

  def main(args: Array[String]): Unit = {

    // Right-align "Ash" in a field 10 characters wide
    val result = f"${"Ash"}%10s"

    println(s"|$result|")
  }
}


/*
run:

|       Ash|

*/

 



answered 3 days ago by avibootz
0 votes
// Zero-padding numbers

object ZeroPaddingExample {

  def main(args: Array[String]): Unit = {

    val number = 123

    // Pad with leading zeros to width 8
    val result = f"$number%08d"

    println(result)
  }
}


/*
run:

00000123

*/

 



answered 3 days ago by avibootz
0 votes
// Formatting a table

object TableExample {

  def main(args: Array[String]): Unit = {

    // Print column headers
    println(f"${"Name"}%-15s${"Age"}%-10s${"Salary"}%-12s")

    // Print data rows
    println(f"${"Paula"}%-15s${36}%-10d${165000.0}%-12.2f")
    println(f"${"Bea"}%-15s${63}%-10d${172000.0}%-12.2f")
  }
}


/*
run:

Name           Age       Salary      
Paula          36        165000.00   
Bea            63        172000.00   

*/

 



answered 3 days ago by avibootz
0 votes
object FixedWidthFunction {

  /**
   * Returns a string padded to a fixed width.
   *
   * value     - value to format
   * width     - field width
   * leftAlign - true for left alignment,
   *             false for right alignment
   */
  def fixedWidth(value: String,
                 width: Int,
                 leftAlign: Boolean = true): String = {

    if (leftAlign)
      s"%-${width}s".format(value)
    else
      s"%${width}s".format(value)
  }

  def main(args: Array[String]): Unit = {

    println("|" + fixedWidth("Hello", 10) + "|")
    println("|" + fixedWidth("123", 10, false) + "|")
  }
}


/*
run:

|Hello     |
|       123|

*/

 



answered 3 days ago by avibootz
edited 3 days ago by avibootz
...