看到一堆的移位操作符作输入输出,我就十分的恼火,用它写代码,它十分希望你是傻瓜,因为是傻瓜式的编程嘛,它对你隐藏了大量的细节,你不去深入细致的了解它的格式输出,你就只能得到这个杯具的结果
但如果你还爱用printf()格式化输入,你可以容易的就知道你输出的样子
printf("%.15lf",a); --- 可以输出小数点后15位,
就算不指定
printf("%lf",a); ---也有默认小数点后6位输出
你用那cout 和 << 输出实型数,不特别指定格式时,就是默认7位有效数字.
对于cout,你会去研究它的特别格式吗?多数人都忽略它们了,我也忽略它的,
即使是用C++ 我依旧爱用printf来格式化
我说,那人写得那么多干嘛?
#include
namespace Numerics {
class Decimal_Place {
public:
Decimal_Place(double d)
{
os.str("");
os.precision(15);
os << d; s = os.str();
pos = s.find('.');
}
const char* str() const { return s.c_str(); }
int before_point() const { return pos; }
int after_point() const { return s.size()- pos - 1; }
int significance() const { return s.size()-1; }
friend std::ostream& operator<<(std::ostream&, const Decimal_Place&);
private:
std::string s; int pos;
static std::ostringstream os;
public:
const Decimal_Place&
show(std::ostream& out,const char* delim) const
{ out << s << delim; return *this; }
}; // Decimal_Place end
std::ostringstream Decimal_Place::os;
这段就够了。
#include
#include
namespace Numerics {
class Decimal_Place {
public:
Decimal_Place(double d)
{
os.str("");
os.precision(15);
os << d; s = os.str();
pos = s.find('.');
}
const char* str() const { return s.c_str(); }
int before_point() const { return pos; }
int after_point() const { return s.size()- pos - 1; }
int significance() const { return s.size()-1; }
friend std::ostream& operator<<(std::ostream&, const Decimal_Place&);
private:
std::string s; int pos;
static std::ostringstream os;
public:
const Decimal_Place&
show(std::ostream& out,const char* delim) const
{ out << s << delim; return *this; }
}; // Decimal_Place end
std::ostringstream Decimal_Place::os;
inline std::ostream&
operator<<(std::ostream& out, const Decimal_Place& cdp)
{ cdp.show(out,"\t"); return out; }
class Demonstrate_Decimal_Place {
protected:
virtual void display_helper(const Decimal_Place& ob)
{
std::cout << ob << "count\n"
<< "before_point: " << ob.before_point() << '\t'
<< "after_point: " << ob.after_point() << '\n';
}
public:
typedef const Decimal_Place& DPR;
typedef void(*Handler)(DPR);
static Handler display(DPR ob) throw()
{ display_helper(ob);
return display_helper; }
static int Test_Decimal_Place() throw()
{ display(3.14159)(32.16); return 0; }
}; // End of class Demonstrate
} // the end of namespace Num;
int main()
{
return
Numerics
::Demonstrate_Decimal_Place
::Test_Decimal_Place();
}
double只有8字节不能无限长的