Creating Format Strings


  • You have printf() and format() methods to print output with formatted numbers. 
  • The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object.
Program

object Demo
{
  def main(args: Array[String])
  {
    var floatvar=12.3456
    var intvar=2000
    var stringvar="hari_taraka_prabhu"
    
    var fs=printf("the value of the float variable is:" + "%f, while the value of integer is " + "variable is %d, and the string is " + "is %s",floatvar,intvar,stringvar)
    println(fs)
    println("                        ")
    
    var sf=printf(s"the value of the float variable is:" + "%f, \nwhile the value of integer is " + "variable is %d, \nand the string is " + "is %s",floatvar,intvar,stringvar)
    println(sf)
    }
}



Output:

the value of the float variable is:12.345600, while the value of integer is variable is 2000, and the string is is hari_taraka_prabhu()
                        
the value of the float variable is:12.345600, 
while the value of integer is variable is 2000, 
and the string is is hari_taraka_prabhu()