如何在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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541381.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

basic knowledge

Position 属性:规定元素的定位类型。即元素脱离文档流的布局,在页面的任意位置显示。 ①absolute :绝对定位;脱离文档流的布局,遗留下来的空间由后面的元素填充。定位的起始位置为最近的父元素(postion不为static)&…

avatar.php uid,phpcms函数库中获取会员头像方法get_memberavatar()有时无效问题

修复方法:首先我先给出无效情况的演示代码,如下:$userid intval($_GET[userid]);$userinfo $this->db->get_one(userid.$userid);$this->db->set_model(10); //原因便在这里$userdetail $this->db->get_one("useri…

ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起

ruby 集合 分组In this program, we will see how we can combine the two sets? This is not a very difficult task. This can be easily done with the help of the operator. In many places of programming, you will find that operator is overloaded for various ty…

​Python中面向对象的编程

Python面向对象的编程1概述(1)面向对象编程面向对象的编程是利用“类”和“对象”来创建各种模型来实现对真实世界的描述,使用面向对象编程的原因一方面是因为它可以使程序的维护和扩展变得更简单,并且可以大大提高程序开发效率&a…

php中用for循环制作矩形,PHP中for循环语句的几种变型

PHP中for循环语句的几种变型2021-01-22 10:21:42406for语句可以说是PHP(同时也是多种语言)的循环控制部份最基本的一个语句了,for语句的执行规律和基础用法在这里就不多说,可以参见PHP手册for语句部分。PHP手册中对它的语法定义如下:for(expr…

c语言用命令行编译运行程序_使用C程序执行系统命令

c语言用命令行编译运行程序Sometimes, we may need to execute Linux/Windows DOS commands through our C program. (Note: the code given below is compiled and executed on Linux GCC compiler, so here we are testing Linux commands only). 有时,我们可能需…

python 熊猫,Python熊猫

我试图连续分组和计算相同的信息:#Functionsdef postal_saude ():global df, lista_solic#List of solicitantes in Postal Saudelist_sol [lista_solic["name1"], lista_solic["name2"]]#filter Postal Saude Solicitantesdf df[(df[Cliente…

Spring的两种任务调度Scheduled和Async

Spring提供了两种后台任务的方法,分别是: 调度任务&#xff0c;Schedule异步任务&#xff0c;Async当然&#xff0c;使用这两个是有条件的&#xff0c;需要在spring应用的上下文中声明<task:annotation-driven/>当然&#xff0c;如果我们是基于java配置的&#xff0c;需要…

建立单链表 单链表的插入_单链列表插入

建立单链表 单链表的插入All possible cases: 所有可能的情况&#xff1a; Inserting at beginning 开始插入 Inserting at the ending 在末尾插入 Inserting at given position 在给定位置插入 Algorithms: 算法&#xff1a; 1)开始插入 (1) Inserting at the beginning) In…

mysql学习笔记(1-安装简介)

mysql的安装方式&#xff1a;(1)通过系统提供的默认版本(rpm包&#xff0c;稳定版&#xff0c;该版本满足了使用的需求&#xff0c;建议使用&#xff0c;os vendor)(2)mysql官方提供官方提供的通用rpm安装包通用二进制格式的程序包(直接下载文件&#xff0c;解压到指定目录&…

存储器间接寻址方式_8086中的数据存储器寻址模式

存储器间接寻址方式In this type of addressing mode, first the offset address is calculated, then the memory address is calculated and then the operand form that memory location is fetched. There are following modes which lie under the Data Addressing Mode: …

oracle asm 删除diskgroup,ASM磁盘组删除DISK操作

没想到这么简单的操作&#xff0c;由于不熟悉还碰到了两个小问题。[oracledbserver1 ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.2.0 Production on Tue Aug 9 10:08:062011Copyright (c) 1982, 2010, Oracle.All rights reserved.Connected to:Oracle Database 11g Ent…

intellij idea 最常用的快捷键

F2&#xff0c; 可以快速的向下跳走 CtrlF7&#xff0c;可以查询当前元素在当前文件中的引用&#xff0c;然后按 F3 可以选择AltQ&#xff0c;可以看到当前方法的声明CtrlP&#xff0c;可以显示参数信息CtrlAltV&#xff0c;可以引入变量。例如&#xff1a;new String(); 自动导…

如何在Java中检查字符串是否为数字?

We will check whether string is a number or not – with the help of logic we will solve this problem, 我们将检查字符串是否为数字-借助逻辑&#xff0c;我们将解决此问题&#xff0c; In the first step, we will take a string variable named str and store any val…

oracle清理告警日志,Oracle 跟踪/告警/监听日志的清理脚本

[root ~]# cat del_oracle_log.sh#!/bin/bashsource /home/oracle/.bash_profilefunction audit_log(){ #---audit_log日志跟踪文件#audit_log$(strings $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora|grep -i audit_file_dest|awk -F {print $NF}|sed "s///g")audit_lo…

python爬虫之scrapy框架

Scrapy是一个为了爬取网站数据&#xff0c;提取结构性数据而编写的应用框架。 其可以应用在数据挖掘&#xff0c;信息处理或存储历史数据等一系列的程序中。其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的&#xff0c; 也可以应用在获取API所返回的数据(例如 Amazon As…

oracle中的事务回滚,ORACLE 死事务的回滚

死事务出现在异常关闭数据库或者事务进程不正常结束&#xff0c;比如KILL -9&#xff0c;shutdown abort的情况下。当前数据库里的死事务可以通过查询内部表x$ktuxe来获得。select ADDR,KTUXEUSN,KTUXESLT,KTUXESQN,KTUXESIZ from x$ktuxe where KTUXECFLDEAD;ADDR …

大数据数据可视化设计原则_数据可视化设计的8顶帽子

大数据数据可视化设计原则8 hats of data visualization are basically the important persons and their roles that are basically required to carry out data visualization are as follows: 数据可视化有8个基本要素&#xff0c;而进行数据可视化所需的基本角色如下&#…

debian8.8安装谷歌浏览器

第一步&#xff1a;下载&#xff1a; wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb //32位 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb //64位第二步&#xff1a;安装dpkg -i google-chrome*.deb…

MB_LEN_MAX常数,带C ++示例

C MB_LEN_MAX宏常量 (C MB_LEN_MAX macro constant) MB_LEN_MAX constant is a macro constant which is defied in climits header, it is used to get the maximum number of bytes in a multibyte character, for any locale, it returns maximum number of bytes that a m…