to_string() is a library function of <string> header, it is used to convert numeric value (number) to string.

to_string()是<string>标头的库函数,用于将数值(数字)转换为字符串。

Syntax:

句法:

string to_string(numberic_value);

Here,

这里,

  • string is the return type i.e. function returns an string object that contains the numeric value in string format.

    string是返回类型,即函数返回一个字符串对象,其中包含字符串格式的数字值。

  • numbric_value is the number which can be integer, float, long, double.

    numbric_value是可以为整数,浮点数,长整数,双精度数的数字。

Example:

例:

#include <iostream>
#include <string>
using namespace std;
int main ()
{//definition of different types of data type
int intVal =12345;
float floatVal = 123.45f;
long longVal = 123456789;
//converting values to string an printing
cout<<"intVal (string format) : "<<to_string (intVal) <<endl;
cout<<"floatVal (string format) : "<<to_string (floatVal) <<endl;
cout<<"floatVal (string format) : "<<to_string (longVal) <<endl;
return 0;
}

Output

输出量

    intVal (string format) : 12345
floatVal (string format) : 123.449997
floatVal (string format) : 123456789
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Expressions results can also be converted to string directly (as the type of expression’s result is numeric)

表达式结果也可以直接转换为字符串(因为表达式结果的类型是数字)

Consider the example:

考虑示例:

#include <iostream>
#include <string>
using namespace std;
int main ()
{cout<<to_string (10+20+30+40) <<endl;
cout<<to_string (10+20+12.34) <<endl;
cout<<to_string (10/20+30*2) <<endl;
return 0;
}

Output

输出量

    10042.34000060

不使用“使用命名空间std”的函数和对象 (Functions and object without using 'using namespace std')

using namespace std is an statement that tells to the compiler to use namespace named std, if we do not write this statement, then we need to use std:: with all functions, objects.

using namespace std是一条语句,告诉编译器使用名为std的命名空间,如果我们不编写此语句,则需要对所有函数,对象使用std :: 。

Consider the example:

考虑示例:

#include <iostream>
#include <string>
int main ()
{std::cout<<std::to_string (10+20+30+40) <<std::endl;
std::cout<<std::to_string (10+20+12.34) <<std::endl;
std::cout<<std::to_string (10/20+30*2) <<std::endl;
return 0;
}

Output

输出量

    10042.34000060

翻译自: https://www.includehelp.com/stl/convert-numeric-to-string-using-string-to-string.aspx

在C ++ STL中使用string :: to_string()将数字转换为字符串相关推荐

  1. QT5中在textEdit上面显示数字、字符串

    QT5中在textEdit上面显示数字.字符串 显示用到textEdit中的函数setText()函数,需要将数字.字符串转换为QString类型才能显示. ui->textEdit->s ...

  2. C++ STL : 模拟实现STL中的string类

    string的文档介绍 string是表示字符序列的类 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作 单字节字符字符串的设计特性. string类是使用c ...

  3. html中转换货币,如何将数字转换为货币vise? (html输入/输出)

    据我所知,你需要两个功能.一个人应该将数字转换为千位分隔的字符串,另一位应该将其转换回常规数字. 因此,这里有如下功能: 转换数为字符串与数千分离 function convertToString(n ...

  4. stl string 函数_使用C ++ STL中的string :: append()函数将文本追加到字符串

    stl string 函数 append() is a library function of <string> header, it is used to append the extr ...

  5. stl取出字符串中的字符_在C ++ STL中使用比较运算符比较两个字符串

    stl取出字符串中的字符 字符串作为数据类型 (String as datatype) In C, we know string basically a character array termina ...

  6. 使用java中的String类操作复杂的字符串

    今天自己出了一道题差点把自己难住-- 题目是这样的: 已知有三条查询的sql语句,小明不小心写一块了,你帮他拆分开.sql语句为:"select * from books where bid ...

  7. 将Excel中存储为文本的数字转换为数字格式

    有时,Exel 2007中的数字可能会作为文本格式化并存储在单元格中,这会给以后的计算造成问题或导致排序次序混乱.您可能在格式设置为文本的单元格中键入了数字,或者数据可能作为文本从外部数据源导入或复制 ...

  8. sql server中的怎么把数值型转换为字符串

    ①select cast(字段 as varchar) from 表名 ②select convert(varchar(50),字段) from 表名

  9. js 中 中文、空格、数字、字符串混合排序

    最近有个需求,需要对后台的数组对象按照某个属性进行排序,属性值可能是 中文.字符串.数字.特殊字符.空字符串,网上大部分都只是针对某一个类型进行排序,在参考这篇博客的基础之上https://blog. ...

最新文章

  1. 算法-----------猜数字大小
  2. 2012-02-25工作记录
  3. (笔试题)最大覆盖点
  4. 采购杀毒软件,你说话能算数么?
  5. C/C++语言宏定义##连接符和符#的使用
  6. oracle更新数据没有变化,往oracle里面写入数据,怎么表空间都没有变化
  7. 阿里云杜欢:云上Serverless开发能力将成为前端的“金手指”
  8. XMLHTTPRequest的属性和方法简介
  9. slf4j mysql_能显示sql语句,没有报错,但是MySQL里没有表?还有,slf4j是必须的吗?...
  10. python again语句_【python笔记 二 】python语句
  11. Linux命令之umask
  12. Java Colections 集合类 —— List、ArrayList、Set(HashSet)
  13. mac上好用的下载器有哪些,mac下载种子用什么软件
  14. 为什么使用一个变频器控制多个电机?
  15. 冲刺倒计时,复习核心是什么?
  16. 无人驾驶到底怎么赚钱?很现实,八仙过海,各显神通
  17. 新版标准日本语中级_第二十三课
  18. 在游戏界,就有了些游戏界的理解。不过本文是对某游戏开发商的信
  19. 腾讯云轻量应用服务器搭建网站
  20. 刷脸自助登记 访客管理不再潦草应对!

热门文章

  1. java 运行 出现选择_Eclipse 运行出现java.lang.NoClassDefFoundError的解决方法
  2. 编写可靠bash脚本的一些技巧
  3. 快准牌电脑发软件_做自媒体必备技能,视频剪辑软件排名(精品篇)
  4. php 导出excel 特殊字符,PHPEXCEL导出,存在特殊字符遇到的问题
  5. eview面板数据之混合回归模型_【视频教程】Eviews系列25|面板数据回归分析之Hausman检验及本章常见问题解答...
  6. ionic保存到mysql_ionic sqlite 存取数据封装(兼容真机与webkit浏览器)
  7. 企业信息化投入中咨询服务_全过程工程咨询服务核心价值是什么
  8. scala通过JDBC进行数据库操作
  9. Real提示“作为受限用户,您无足够的windows操作权限”的解决办法
  10. C++类型萃取之type_traits和type_info