如何在Scala中将Double转换为String?

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

Scala中的Double是一种数据类型,用于存储带有小数的数值。 它可以存储一个64位浮点数。

Example:

例:

    val  decimalVal : Double = 561.093

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

Scala中的String是不可变的字符序列。 它可以存储字符。

Example:

例:

    val stringVal : String = "Includehelp"

在Scala中将double转换为字符串 (Convert double to string in Scala)

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

我们可以使用多种方法在Scala中将双精度值转换为字符串值,

  1. Using String.format() method

    使用String.format()方法

  2. Using String.valueOf() method

    使用String.valueOf()方法

  3. Using toString method

    使用toString方法

1)使用String.format()方法将double转换为字符串 (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.

String类的String.format()方法用于将double转换为string。 使用此方法,可以指定在转换为字符串时要考虑的小数总数。 如果所考虑的小数位数小于为字符串指定的数字,则将对数值进行舍入。

Syntax:

句法:

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

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

程序使用String.format()方法将双精度转换为字符串

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.

在上面的代码中,我们创建了一个名为convertDoubleToString()的函数,该函数接受双精度值作为参数并返回转换后的字符串。 在函数中,我们使用String.format()方法将double值转换为string并返回它。

2)使用String.valueOf()方法将double转换为字符串 (2) Convert double to string using String.valueOf() method)

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

String.valueOf()方法用于将双精度值转换为字符串。

Syntax:

句法:

    val string_name = String.valueOf(double_value)

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

程序使用String.valueOf()方法将双精度转换为字符串

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.

在上面的代码中,我们创建了一个名为convertDoubleToString()的函数,该函数接受双精度值作为参数并返回转换后的字符串。 在函数中,我们使用String.valueOf()方法将双精度值转换为字符串并返回它。

3)使用toString方法将double转换为字符串 (3) Convert double to string using toString method)

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

Scala中的toString方法用于将双精度值转换为字符串。

Syntax:

句法:

    val string_value = double_value.toString

Program to convert double to string using toString method

程序使用toString方法将双精度转换为字符串

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.

在上面的代码中,我们创建了一个名为convertDoubleToString()的函数,该函数接受双精度值作为参数并返回转换后的字符串。 在函数中,我们使用toString方法将双精度值转换为字符串并返回它。

翻译自: https://www.includehelp.com/scala/how-to-convert-double-to-string-in-scala.aspx

    原文作者:cumt951045
    原文地址: https://blog.csdn.net/cumt951045/article/details/107796383
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞