首页 > 解决方案 > c++ 将打印格式从整数 2255 更改为 $xx.xx

问题描述

我有函数 return int i = 2255 这意味着我的口袋里有多少美分,我想以 $xx.xx 格式打印它如何打印到 22.55?非常感谢

标签: c++

解决方案


printf("$%d.%d", i / 100, i % 100);

或者

printf("$%0.2f", double(i) / 100);

不过,在 C++11 及更高版本中,请考虑改用std::coutwith std::put_money()

#include <iostream>
#include <iomanip>

std::cout << std::put_money(double(i) / 100);

推荐阅读