c++ - c++ 将打印格式从整数 2255 更改为 $xx.xx
问题描述
我有函数 return int i = 2255 这意味着我的口袋里有多少美分,我想以 $xx.xx 格式打印它如何打印到 22.55?非常感谢
解决方案
printf("$%d.%d", i / 100, i % 100);
或者
printf("$%0.2f", double(i) / 100);
不过,在 C++11 及更高版本中,请考虑改用std::cout
with std::put_money()
:
#include <iostream>
#include <iomanip>
std::cout << std::put_money(double(i) / 100);
推荐阅读
- mongodb-query - 与 mongoshell 的更新倍数等效的 Java 驱动程序
- swift - 如何在alamofire中分离请求体?
- node.js - react和API(nodejs)之间的通信
- c# - 下载后如何打开 Word/Excel 文件
- android-studio - Android Studio 无法在布局编辑器中放大/缩小
- javascript - Android Chrome 上的 HTML5 游戏手柄 API
- c# - WebAPI 是否可以在操作中发布 IFormCollection 和对象?
- python - Python:从列表列表中检索项目
- ios - cocoapods 构建设置会覆盖所有项目目标设置吗?
- php - 从数组中删除与php中特定值匹配的元素