How to convert Double to String in Scala?

Scala | Conversion from Double to String: Here, we are going to learn how to convert double to string in Scala, all methods to convert to string with examples.
Submitted by Shivang Yadav, on May 23, 2020

Double in Scala is a data type that stores numerical values that have decimals. It can store a 64-bit floating point number.

Example:

    val  decimalVal : Double = 561.093

String in Scala is a sequence of characters that is immutable. It can store characters.

Example:

    val stringVal : String = "Includehelp"

Convert double to string in Scala

We can convert a double value to a string value in Scala using multiple methods,

  1. Using String.format() method
  2. Using String.valueOf() method
  3. Using toString method

1) Convert double to string using String.format() method

The String.format() method of the String class is used to convert double to string. Using this method you can specify the total number of decimals that we want to consider while converting to string. If the decimal places considered is less than the number specified for the string, rounding of values takes place.

Syntax:

    val string_name = String.format("decimal_places", double_name)

Program to convert double to string using String.format() method

object MyObject {
    def convertDoubleToString(doubleVal : Double): String = {
        return String.format("%.5f", doubleVal)
    }
    
    def main(args: Array[String]) {
        val dbVal : Double = 59.1295
        println("The String converted decimal is " + convertDoubleToString(dbVal))
    }
}

Output:

The String converted decimal is 59.12950

Explanation:

In the above code, we have created a function named convertDoubleToString() that accepts double value as an argument and returns the converted string. In the function, we have used the String.format() method to convert the double value to string and returns it.

2) Convert double to string using String.valueOf() method

The String.valueOf() method is used to convert a double value to string.

Syntax:

    val string_name = String.valueOf(double_value)

Program to convert double to string using String.valueOf() method

object MyObject {
    def convertDoubleToString(doubleVal : Double): String = {
        return String.valueOf(doubleVal)
    }
    
    def main(args: Array[String]) {
        val dbVal : Double = 98.7415
        println("The String converted decimal is " + convertDoubleToString(dbVal))
    }
}

Output:

The String converted decimal is 98.7415

Explanation:

In the above code, we have created a function named convertDoubleToString() that accepts double value as an argument and returns the converted string. In the function, we have used the String.valueOf() method to convert the double value to string and returns it.

3) Convert double to string using toString method

The toString method in Scala is used to convert the double value to string.

Syntax:

    val string_value = double_value.toString

Program to convert double to string using toString method

object MyObject {
    def convertDoubleToString(doubleVal : Double): String = {
        return doubleVal.toString
    }
    
    def main(args: Array[String]) {
        val dbVal : Double = 123.09
        println("The String converted decimal is " + convertDoubleToString(dbVal))
    }
}

Output:

The String converted decimal is 123.09

Explanation:

In the above code, we have created a function named convertDoubleToString() that accepts double value as an argument and returns the converted string. In the function, we have used the toString method to convert the double value to string and return it.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.