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

如何在Scala中将Double转换为String?相关推荐

  1. 如何在Java中将double转换为int?

    在本文中,我们将看到如何将double转换为int. 在Java编程中,您将有一个double原语值(例如82.14),但是要执行进一步的操作,您需要一个int值(例如82),所以让我们看看如何在Ja ...

  2. scala 转换为字符串_如何在Scala中将字符串转换为布尔值?

    scala 转换为字符串 String in Scala is a sequence of characters. In Scala, the String object is immutable. ...

  3. scala hashmap_如何在Scala中将Hashmap转换为Map?

    scala hashmap Let's first understand what are maps and hashmaps? 首先让我们了解什么是map和hashmap ? map in Scal ...

  4. scala 函数中嵌套函数_如何在Scala中将函数转换为部分函数?

    scala 函数中嵌套函数 First, let's see what is a function and a partial function, and then we will see their ...

  5. android double转string_如何使用Java程序将Double转换为String

    在Java中将String转换为Double 至少有三种方法可以将表示double值的String 转换为Double对象.可能还有多种方法可以做同样的事情,如果你了解此处还有未列出的其他转换方法,请 ...

  6. java 字符串 转double_如何使用Java程序将Double转换为String

    在Java中将String转换为Double 至少有三种方法可以将表示double值的String 转换为Double对象.可能还有多种方法可以做同样的事情,如果你了解此处还有未列出的其他转换方法,请 ...

  7. 如何在JavaScript中将十进制转换为十六进制

    如何在JavaScript中将十进制值转换为等效的十六进制值? #1楼 如果要将数字转换为RGBA颜色值的十六进制表示形式,我发现这是以下几个技巧中最有用的组合: function toHexStri ...

  8. 在Java中将Double转换为Integer

    本文翻译自:Cast Double to Integer in Java Any way to cast java.lang.Double to java.lang.Integer ? 有什么方法可以 ...

  9. java中long如何使用_如何在Java中将long转换为int?

    问题 如何在Java中将long转换为int? #1 热门回答(218 赞) 简单类型转换应该这样做: long l = 100000; int i = (int) l; 但请注意,大数(通常大于21 ...

最新文章

  1. [face_recognition中文文档] 第3节 用法
  2. 线程池状态和使用注意点
  3. matlab 按一定概率取数组中的值
  4. FMS 中文帮助 (下载)
  5. eclipse 的project explorer问题,这个怎样把localFileSystem去掉,
  6. 易语言 存储过程 mysql_在易语言中调用MS SQL SERVER数据库存储过程(Transact-SQL)方法总结...
  7. RocketMQ源码学习(六)-Name Server
  8. Nginx源码分析 - 主流程篇 - 解析配置文件(13)
  9. qt5使用触屏 偶尔没响应_【电凝推荐】适合医学生使用的笔记本电脑20200519
  10. Alibaba Fastjson 入门详细教程
  11. 从海康7816的ps流里获取数据h264数据
  12. 设备指纹技术分析和应用分析
  13. 『迷你教程』数据分析都不知道的非参数统计概论
  14. jquery实现页面提示,数据正在加载中
  15. FastDFS文件存储系统
  16. php验证码刷新_php实现点击可刷新验证码
  17. vega56刷64_AMD Vega 56显卡能刷成Vega 64真相了
  18. iOS App 名字国际化的办法
  19. 上帝开了一间药房 ,只卖水果不卖药!
  20. 支付宝VR Pay,马云爸爸带来的又一个VR购物惊喜

热门文章

  1. java中的显示初始化和特定初始化
  2. android开发屏幕横放,android-即使从横向旋转到垂直,细节片段也会...
  3. com.sec.android.app.smartclipservice,EPR Aerospace News
  4. 语音对讲软件_三款语音转文字工具,语音输入,高效转换,准确率高
  5. apache启动失败_请检查相关配置.√mysql5.1已启动._1、Apache启动失败,请检查相关配置-百度经验...
  6. c++ python混合编程 restful_简单上手nodejs调用c++(c++和js的混合编程)
  7. 照片打印预览正常打印空白_小米发布口袋照片打印机,可无墨打印3寸背胶照片...
  8. os.environ[‘CUDA_VISIBLE_DEVICES‘]= ‘0‘设置环境变量
  9. 2017 到 2018,PWA 技术到底经历了什么
  10. RxJS + React hooks