实战c++中的string系列--指定浮点数有效数字并转为string
上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换。
还是处于控件显示的原因。比方说要显示文件的大小,我们从server能够获得这个文件的总bytes。这样就须要我们依据实际情况是显示bytes、kb、mb等单位。
经常使用的做法就是把num_bytes/1024,这个时候往往会得到浮点型。浮点型转string也没问题,可是假设你须要保留这个浮点型的一位或是几位小数,怎么操作会方便快捷呢?
你进行了相关搜索。可是非常多人给你的回答都是要么使用cout, 要么使用printf进行格式化输出。
我们使用的是stringstream
Stringstreams allow manipulators and locales to customize the result of these operations so you can easily change the format of the resulting string
#include <iomanip>
#include <locale>
#include <sstream>
#include <string> // this should be already included in <sstream>
// Defining own numeric facet:
class WithComma: public numpunct<char> // class for decimal numbers using comma instead of point
{
protected:
char do_decimal_point() const { return ','; } // change the decimal separator
};
// Conversion code:
double Number = 0.12; // Number to convert to string
ostringstream Convert;
locale MyLocale( locale(), new WithComma);// Crate customized locale
Convert.imbue(MyLocale); // Imbue the custom locale to the stringstream
Convert << fixed << setprecision(3) << Number; // Use some manipulators
string Result = Convert.str(); // Give the result to the string
// Result is now equal to "0,120"
setprecision
控制输出流显示浮点数的有效数字个数 。假设和fixed合用的话,能够控制小数点右面的位数
可是这里须要注意的是头文件:
#include <iomanip>
实战c++中的string系列--指定浮点数有效数字并转为string的更多相关文章
- 实战c++中的vector系列--知道emplace_back为何优于push_back吗?
上一篇博客说道vector中放入struct.我们先构造一个struct对象.再push_back. 那段代码中,之所以不能使用emplace_back,就是由于我们定义的struct没有显示的构造函 ...
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...
- 实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)
stl算法中有个copy函数.我们能够轻松的写出这种代码: #include <iostream> #include <algorithm> #include <vect ...
- 实战c++中的vector系列--将迭代器转换为索引
stl的迭代器非常方便 用于各种算法. 可是一想到vector.我们总是把他当做数组,总喜欢使用下标索引,而不是迭代器. 这里有个问题就是怎样把迭代器转换为索引: #include <vecto ...
- 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)
之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...
- 实战c++中的vector系列--构造、operator=和assign差别
vector或许是实际过程中使用最多的stl容器.看似简单,事实上有非常多技巧和陷阱. 着重看一看vector的构造,临时依照C++11: default (1) explicit vector (c ...
- 实战c++中的vector系列--creating vector of local structure、vector of structs initialization
之前一直没有使用过vector<struct>,如今就写一个简短的代码: #include <vector> #include <iostream> int mai ...
- 实战c++中的vector系列--emplace_back造成的引用失效
上篇将了对于struct或是class为何emplace_back要优越于push_back,可是另一些细节没有提及.今天就谈一谈emplace_back造成的引用失效. 直接撸代码了: #inclu ...
- 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())
关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了. 是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为 ...
随机推荐
- Sqlserver添加加字段、删除字段、修改字段类型、修改字段名、修改字段默认值
参考:https://www.cnblogs.com/pangpanghuan/p/6432331.html 初始化表: --.添加字段 --1.1.为null alter table DataTab ...
- 对数组内容使用了json_encode返回汉字内容返回了空值
如果使用json_encode对数组进行转成JSON字符串时候,发现汉字的全部为空,这样可以说明的一点是你的页面上用的一定不是UTF8编码,在PHP手册中对json_encode中待编码的值已经说明所 ...
- 01-mysql中的数据类型
mysql中的列数据类型:数值型.字符串类型.日期/时间类型3种 几种列类型描述使用了下述惯例:· M #表示最大显示宽度.最大有效显示宽度是255.· D #适用于浮点和定点类型,表示小数点后面的位 ...
- 周三面试Python开发,这几道Python面试题差点答错,Python面试题No7
第1题:阅读下面的代码,默读出A0,A1至An的最终值. A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5))) A1 = range(10) A2 = [ ...
- AnjularJs-Select添加数据并设置默认值
html中 <select name="book_classify" class="all_list_btn" ng-model="select ...
- LeetCode(10) Regular Expression Matching
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- 分享一段代码,关于List
@SuppressWarnings("unchecked") public List<LogisticsOrderType> getAllLogisticsOrderT ...
- hexo干货系列:(二)hexo主题下载及配置
前言 上一篇文章介绍了hexo+gitHub简易搭建属于自己的个人独立博客,但是主题是默认的landscape,略显简单,今天的教程推荐Jacman主题. Jacman是一款为Hexo打造的一款扁平化 ...
- css装饰文本框input
在web程序前端页面中,<input>恐怕是用的最多的html元素了,各个需要录入信息的场合都会用到它,一般都会用css来修饰一下使得它更好看. 原始的不加修饰的文本框像下面,有些单调,页 ...
- F题
Problem F Codeforces 16E 这道题是一道数位Dp将鱼的死活列为0两种状态然后找DP关系 •题意:有n(n<=18)条鱼,接下来的n-1天,每天会有一对鱼(a,b)相遇,每天 ...