1

In C++, I'm using the following statements to display output:

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

My output looks like

0.66

How can I prevent the zero before the decimal from being displayed? I want:

.66
Elliott
  • 360
  • 1
  • 4
  • 15

2 Answers2

0
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);
}
Brandon
  • 22,723
  • 11
  • 93
  • 186
  • Thanks! I hadn't thought about using a string. Thought there might be some kind of setf modifier for this. – Elliott Oct 07 '13 at 00:36
0

One possible solution is converting x to string (more options) and cut off everything before the decimal.

Community
  • 1
  • 1
stuhlo
  • 1,479
  • 9
  • 17