Sometimes in Competitive programming, it is essential to print the output in a given specified format. Most users are familiar with printf function in C. Let us see discuss how we can format the output in Scala. When a String contains values as well as texts succeeding it then formatting is required for changing values and adding texts enclosing it.
Example :
I have written 30 articles
Note:
- In Scala Formatting of strings can be done utilizing two methods namely format() method and formatted() method.
- These methods have been approached from the Trait named StringLike.
Format method can be utilized for format strings and you can even pass parameters to it, where %d is used for Integers and %f is used for floating-points or Doubles.
Example :
// Scala program of format // method // Creating object object GFG { // Main method def main(args: Array[String]) { // Creating format string val x = "There are %d books and" + "cost of each book is %f" // Assigning values val y = 15 val z = 345.25 // Applying format method val r = x.format(y, z) // Displays format string println(r) } } |
There are 15 books and cost of each book is 345.250000
Example :
// Scala program of format for // strings and characters. // Creating object object GFG { // Main method def main(args: Array[String]) { // Creating format string val x = "Geet%c is a %s." // Assigning values val a = 'a' val b = "coder" // Applying format method val r = x.format(a, b) // Displays format string println(r) } } |
Geeta is a coder.
Here, %s is used for strings and %c is used for characters.
This method can be utilized on Integer, Double as well as Strings, as it can apply on any object.
Example :
// Scala program for formatted // method // Creating object object GFG { // Main method def main(args: Array[String]) { // Assigning values val x = 30 // Applying formatted method val r = x.formatted("I have written %d articles.") // Displays format string println(r) } } |
I have written 30 articles.
Here, format string is passed as an argument in the formatted method.
Recommended Posts:
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator mkString() method with a start, a separator and an end with example
- Scala Map mkString() method with a start, a separator and an end with example
- Scala Map addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with a start, a separator and an end with example
- Scala List addString() method with a start, a separator and an end with example
- Scala List mkString() method with a start, a separator and an end with example
- Scala SortedMap mkString() method with a start, a separator and an end with example
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Scala Map size() method with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala Set &() method with example
- Scala Int <(x: Char) method with example
- Scala Int <=(x: Double) method with example
- Scala Int <=(x: Byte) method with example
- Parameterless Method in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

