如何在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.093String 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中將雙精度值轉換為字符串值,
Using String.format() method
使用String.format()方法
Using String.valueOf() method
使用String.valueOf()方法
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.1295println("The String converted decimal is " + convertDoubleToString(dbVal))} }Output:
輸出:
The String converted decimal is 59.12950Explanation:
說明:
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.7415println("The String converted decimal is " + convertDoubleToString(dbVal))} }Output:
輸出:
The String converted decimal is 98.7415Explanation:
說明:
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.toStringProgram 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.09println("The String converted decimal is " + convertDoubleToString(dbVal))} }Output:
輸出:
The String converted decimal is 123.09Explanation:
說明:
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
總結
以上是生活随笔為你收集整理的如何在Scala中将Double转换为String?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 量词逻辑量词里面的v表示?_代理知识表示
- 下一篇: kotlin 两个数字相加_Kotlin