1.C++中使用的是:头文件 #include
(1)setprecision(保留的位数)
(2)setiosflags(ios::fixed)和上面得setprecision(int num)混合使用的话,表示的是保留小数点后num位,在后面所有的cout输出的数据都会按照这个要求进行输出,如果需要改变则再重新设置setprecision后面的数据就行。
iso::fixed 是操作符setiosflags 的参数之一,该参数指定的动作是以带小数点的形式表示浮点
#include<stdlib.h>
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
double a = 10.276689;
cout << setiosflags(ios::fixed);
cout << setprecision(3);
cout << a << endl;
cout << 1.088888 << endl;
cout << "***********************************" << endl;
cout << setprecision(4);
cout << a << endl;
cout << 1.088888 << endl;
return 0;
}
2. java中用DecimalFormat类来进行控制输出的格式。
DecimalFormat dl=new DecimalFormat();
String format=new String("00");
dl.applyPattern(format);
System.out.println(dl.format(yearN)+" ");
上面得"00"表示的就是数据输出的格式,一定是两位整数,如果只有一位数那就会自动在前面补上0。