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 [Last updated : March 10, 2023]

Scala – Double Type

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

    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"

Scala –Double to String Conversion

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)

Scala code 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)

Scala code 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

Scala code 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.

Scala String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.