防止像这样格式化大数字"24.000.000"
Prevent a big number from being formated like this "24.000.000"
我在我的ostream
中插入了一些int
s,但大数字或甚至年份,如3000
或25000123
正在像这样格式化3.000
或25.000.123
。
我不确定为什么会发生这种情况。这可能是因为我在流上使用了imbue("")
,所以十进制数字像这样显示14,53
而不是14.53
,但是我注释了那行,所以一切都保持原样。
我只是想在输出中获得数字时去掉那些点(但我也不想去掉小数点逗号)。我该怎么做呢?
我想也许iomanip
库会有所帮助,但我没有找到任何关于这种情况的东西。
std::ostream& operator <<(std::ostream& os, const Article& a) {
os << a.title() << a.pub_date().year() << ". " << a.price() << "";
return os;
}
如果您为数字指定了自定义分组,则可以继续使用imbue
。
请看下面的例子:http://www.cplusplus.com/reference/locale/numpunct/grouping/
在这个例子中,我们有这段代码,它验证即使设置了区域设置也不进行分组。
// numpunct::grouping example
#include <iostream> // std::cout
#include <string> // std::string
#include <locale> // std::locale, std::numpunct, std::use_facet
// custom numpunct with grouping:
struct my_numpunct : std::numpunct<char> {
// the zero by itself means do no grouping
std::string do_grouping() const {return " ";}
};
int main() {
std::locale loc (std::cout.getloc(),new my_numpunct);
std::cout.imbue(loc);
std::cout << "one million: " << 1000000 << 'n';
return 0;
}
相关文章:
- 是否可以像这样编写c malloc函数代码
- 为什么我不能像这样在静态成员函数中调用静态成员变量?
- 对于像C++这样的现实世界语言,时间复杂性有什么一致的定义吗
- 澄清了 strcpy() 在像这样初始化字符数组时的工作 *Arr.
- 为什么我不能像这样直接将 int 分配给 int 指针:int *p = 6;?
- 如何将像'A'这样的单个字符替换为类似"10"?
- 为什么我不能像这样C++访问类的对象?
- 为什么这个指针到指针的二维数组像这样分配新的 int
- 像这样的PIMPL实现是否有任何简单的语法
- 使用输入重载时,是否可以像这样退出 while 循环
- 像这样的 for 循环被认为是不好的做法吗?
- 为什么 std::experimental::filesystem::p ath 不接受像 "*" 这样的通配符?
- 像这样写作业有什么问题
- 不应该像这样从 stl::list 中删除进入无限循环
- 为什么我不能像这样擦除矢量的所有元素?
- 为什么我不能像这样初始化结构
- 像这样在堆栈上创建一个小的像素图可以吗
- 是否可以像这样使操作员过载<<?
- 防止像这样格式化大数字"24.000.000"
- 如何使用std::cout停止输出像5.0这样的数字的小数和零?