fork download
  1. #include <iostream>
  2. #include <locale>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. class my_put : public std::num_put<char> {
  7. iter_type do_put (iter_type out, ios_base& str, char_type fill, double val) const
  8. {
  9. streamsize prec = str.precision();
  10. if((str.flags() & ios_base::floatfield) == ios_base::fixed && fabs(val)*2 <= pow(10.0, -(int)prec))
  11. str.precision(0);
  12. out = std::num_put<char>::do_put(out, str, fill, val);
  13. str.precision(prec);
  14. return out;
  15. }
  16. };
  17.  
  18. int main()
  19. {
  20. double a[] = {123, 0, 0.001, 0.0001 };
  21. cout.imbue(locale(cout.getloc(), new my_put));
  22. cout.precision(3);
  23. cout.setf(ios_base::fixed, ios_base::floatfield);
  24. for(int i = 0; i < 4; ++i)
  25. cout << a[i] << '\n';
  26. }
Success #stdin #stdout 0.01s 2816KB
stdin
Standard input is empty
stdout
123.000
0
0.001
0