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,
- Using String.format() method
- Using String.valueOf() method
- 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:
Program to convert double to string using String.format() method
Output:
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:
Program to convert double to string using String.valueOf() method
Output:
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:
Program to convert double to string using toString method
Output:
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.