gpt4 book ai didi

c++ - 防止在C++中显示小数点前的零

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:16 24 4
gpt4 key购买 nike

在 C++ 中,我使用以下语句来显示输出:

// example
float x = 0.66004;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << x;

我的输出看起来像

0.66

如何防止显示小数点前的零?我要:

.66

最佳答案

std::string Foo = std::to_string(0.553);
std::size_t it = Foo.find(".");

if (it != std::string::npos)
{
std::cout<<Foo.erase(0, it);
}

关于c++ - 防止在C++中显示小数点前的零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19214421/

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