▚ 01 C++设置double精度
1.0 问题描述:
📢 在
C++
中,当double类型变量的有效位数超出6时,在默认情况下仅保留6位有效数字。
📢 而某些情况下,我们想要显示该double变量的全部有效位,且避免使用科学计数法的形式(不使用e+或e-)。
- 测试代码:
#include <iostream>
using namespace std;
int main()
{
double x = 0.123456789;
double y = 123456789;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
return 0;
}
- 输出结果为:
x: 0.123457
y: 1.23457e+08
1.1 setprecision()
🍱
setprecision(n)
:表示保留的有效位为n,也即从最高非零位向低位至n个,最后的n位数为四舍五入类型。
🍜setprecision(n)
:不能避免使用科学计数法的形式。
🍝使用setprecision()
需要添加#include <iomanip>
。
🎂使用numeric_limits
需要添加#include <limits>
。
- 测试程序:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main()
{
double x = 0.123456789123456789;
double y = 123456789123456789;
cout << "[1] Default precision (6) " <<endl;
cout << "x: " << x << ", y: " << y << endl;
cout << "[2] Set precision(8) " <<endl;//有效数字为8位
cout << setprecision(8) << "x: " << x << ", y: " << y << endl;
cout << "[3] Max precision(18) " <<endl;
cout << setprecision(std::numeric_limits<long double>::digits10) << "x: " << x << ", y: " << y << endl;
return 0;
}
- 输出结果为:
[1] Default precision (6)
x: 0.123457, y: 1.23457e+17
[2] Set precision(8)
x: 0.12345679, y: 1.2345679e+17
[3] Max precision(18)
x: 0.123456789123456784, y: 123456789123456784
1.2 fixed
🍓
fixed + precision(n)
: 表示小数点后有n位。
🥭可避免使用科学计数法的形式。
🍎使用setprecision()
需要添加#include <iomanip>
。
🍉使用numeric_limits
需要添加#include <limits>
。
- 测试程序:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main()
{
double x = 0.123456789123456789;
double y = 123456789123456789;
cout.flags(ios::fixed);
cout << "[1] Default precision (6) " <<endl;
cout << "x: " << x << ", y: " << y << endl;
cout << "[2] Set precision(8) " <<endl;//小数点后包含8位
cout << setprecision(8) << "x: " << x << ", y: " << y << endl;
cout << "[3] Max precision(18) " <<endl;
cout << setprecision(std::numeric_limits<long double>::digits10) << "x: " << x << ", y: " << y << endl;
return 0;
}
- 输出结果为:
[1] Default precision (6)
x: 0.123457, y: 123456789123456784.000000
[2] Set precision(8)
x: 0.12345679, y: 123456789123456784.00000000
[3] Max precision(18)
x: 0.123456789123456784, y: 123456789123456784.000000000000000000
🥂以下的两种表示方法的效果一致:
//方法一:
cout.flags(ios::fixed);
cout << setprecision(2) << x << endl;
//方法二:
cout << fixed << setprecision(2) << x << endl;
1.3 setw()
🌻
setw(n)
:设置字段宽度。当变量的输出字段小于n时,在该字段前使用空格补充;当变量的输出字段大于n时,则全部输出。
- 测试程序:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string str = "Apple";
cout << str << endl;
cout << setw(10) << str << endl;
return 0;
}
- 输出结果为:
Apple
Apple
▚ 02 Java设置double精度
🍁
String.format(n, number)
:输出number小数点后的n位。若number小数点后的数字小于n,则补充0;若大于n,则四舍五入至n位。
- 测试程序:
public class test_format {
public static void main(String[] args) {
double n = 12.123456789;
double m = 12.12;
System.out.println("[origin] n: " + n);
System.out.println("[format(6)] n: " + String.format("%.6f",n));
System.out.println("[origin] m: " + m);
System.out.println("[format(3)] m: " + String.format("%.3f",m));
}
}
- 输出结果为:
[origin] n: 12.123456789
[format(6)] n: 12.123457
[origin] m: 12.12
[format(3)] m: 12.120