我想写一个方法,它接受一个整数并返回一个用逗号格式化的整数的 std::string
。
示例声明:
std::string FormatWithCommas(long value);
示例用法:
std::string result1 = FormatWithCommas(7800);
std::string result2 = FormatWithCommas(5100100);
std::string result3 = FormatWithCommas(201234567890);
// result1 = "7,800"
// result2 = "5,100,100"
// result3 = "201,234,567,890"
用逗号将数字格式化为 string
的 C++ 方法是什么?
(奖金也将处理 double
s。)
原文由 User 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用
std::locale
与std::stringstream
免责声明: 可移植性可能是一个问题,您可能应该查看通过
""
时使用的语言环境