gpt4 book ai didi

c++ - 限制固定输出的尾随零数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:20 26 4
gpt4 key购买 nike

我需要一些有关使用 C++ 流进行输出格式化的帮助。我想打印带有固定小数点且最多 2 个尾随位置的数字。我尝试了以下方法:

#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char **argv)
{
float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 };

std::cout << std::setprecision(2) << std::fixed;

for(int i = 0; i < 6; ++i)
{
std::cout << testme[i] << std::endl;
}

return 0;
}

输出是:

0.12
1.23
12.35
123.45
1234.50
12345.00

但我想拥有

0.12
1.23
12.35
123.45
1234.5
12345

我能否在不使用额外的字符串操作的情况下实现这一点?

最佳答案

我不知道适合的操纵器,因此您可以使用:

#include <iostream>
#include <iomanip>
#include <cmath>

template <typename T>
struct Fixed
{
const T& value;
const unsigned precision;
const T significant;

Fixed(const T& value, unsigned precision)
: value(value), precision(precision), significant(std::pow(10, precision))
{}

void write(std::ostream& stream) const {
// Adjust stream settings
std::ostream::char_type restore_fill = stream.fill('0');
std::ios_base::fmtflags restore_flags = stream.setf(
std::ios_base::fixed, std::ios_base::floatfield);
std::streamsize restore_precision = stream.precision(0);

// Split the floating point into an integral and rounded fractional part
T integral;
unsigned long fractional = std::round(significant * std::modf(value, &integral));

// Determine the length of the fractional part
unsigned digits = precision;
while(fractional && fractional % 10 == 0) {
fractional /= 10;
--digits;
}

// Carry over to the integral part
if( ! digits && fractional) {
integral += 1;
fractional = 0;
}

// Output
stream << integral;
if(fractional) {
stream << '.' << std::setw(digits) << fractional;
}

// Restore stream settings
stream.precision(restore_precision);
stream.flags(restore_flags);
stream.fill(restore_fill);
}
};

template <typename T>
inline Fixed<T> fixed(const T& value, unsigned precision) {
return Fixed<T>(value, precision);
}

template <typename T>
inline std::ostream& operator << (std::ostream& stream, const Fixed<T>& value) {
value.write(stream);
return stream;
}

int main(int argc, char **argv)
{
float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 };
for(int i = 0; i < 6; ++i)
{
std::cout << fixed(testme[i], 2) << std::endl;
}

return 0;
}

关于c++ - 限制固定输出的尾随零数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com