Scala | Format and Formatted Method Last Updated : 29 Mar, 2019 Comments Improve Suggest changes Like Article Like Report 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 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 // 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) } } Output: There are 15 books and cost of each book is 345.250000 Example : Scala // 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) } } Output: Geeta is a coder. Here, %s is used for strings and %c is used for characters. Formatted method This method can be utilized on Integer, Double as well as Strings, as it can apply on any object. Example : Scala // 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) } } Output : I have written 30 articles. Here, format string is passed as an argument in the formatted method. Comment More infoAdvertise with us Next Article Scala | Format and Formatted Method N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Method Similar Reads Scala Float /(x: Float) method with example The /(x: Float) method is utilized to return the quotient when the specified first float value is divided by second float value. Method Definition: (First_Float_Value)./(Second_Float_Value) Return Type: It returns the quotient when the specified first float value is divided by second float value. Ex 1 min read Scala Float >(x: Float) method with example The >(x: Float) method is utilized to return true if the first float value is greater than the specified second float value. Method Definition: (First_Float_Value).>(Second_Float_Value) Return Type: It returns true if the first float value is greater than the specified second float value. Exam 1 min read Scala | Method Invocation Method Invocation is a technique that demonstrates different syntax in which we dynamically call methods of a class with an object. The naming conventions of Scala are same as of Java, that are:- There should not be any space between the invocation object/target and the dot(.) nor a space between th 3 min read Scala Int +(x: Float) method with example The +(x: Float) method is utilized to return the sum of the specified int value and float value. Method Definition: (Int_Value).+(Float_Value) Return Type: It returns the sum of the specified int value and float value. Example #1: Scala // Scala program of Int +(x: Float) // method // Creating objec 1 min read Scala Char >=(x: Float) method with example The >=(x: Float) method is utilized to find if the stated character value is greater than or equal to 'x' or not. And the type of 'x' must be Float. Method Definition: def >=(x: Float): Boolean Return Type: It returns true if the stated character value is greater than or equal to "x" else it r 1 min read Like