is there any way to remove the zero before the decimal point?
instead of 0.33
it would print .33
is there any way to remove the zero before the decimal point?
instead of 0.33
it would print .33
sure, use:poorly stated questions give you poor answers.Code:cout << ".33";
however, here is an alternative:maybe create a function that takes in a float and returns the string with the leading character (assumed to be a zero) removed?Code:#include<iostream> #include<sstream> using namespace std; int main() { float x = 0.33; stringstream ss; ss << x; cout << ss.str().substr(1,ss.str().length()) << endl; }
Code:std::string a("0.33"); a = a.substr(a.find("."));
What about input such as 10.23 or 1023?
This might also have locale issues (e.g where , is used for decimal point).Code:std::size_t pos = a.find('.'); if (pos == 1 && a[0] == '0') { a = a.substr(pos); }
If C++ accepts ".33" as input for a double then it is a legitimate question whether there is a format flag that lets you output without leading 0 too (I don't know of that).
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.