【double设置精度】C++/Java中double类型设置显示的精度, setprecision, fixed, setw || String.format

13 篇文章 1 订阅

▚ 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





在这里插入图片描述


在这里插入图片描述

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值