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

标签 c++ output setf

在 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/

相关文章:

可变模板参数 : why isn't this working 上的 C++11 初始化程序列表

c++ - 接受 char* 的模板化函数

common-lisp - setf 如何在幕后工作?

macros - 带有运算符参数的定义-修改-宏

java - Android NDK 中的某些 C++ 包含文件不起作用

c++ - 如何从 C++ 中的基类构造函数调用派生类方法?

iphone - 在 iOS 应用程序中创建另一个具有不同内容的输出设备

javascript - 将函数传递给innerHTML方法

c - 关于C中的文件输入和输出

clojure - Clojure 中的 setf