double或Double取整或保留n位小数(后续如果有需要再整理其他小数类型)

1. double或Double取整或保留n位小数

1.1 double取整

1.1.1 Math.floor()向下取整

功能说明:
  • floor这个单词用作名词可以表示为地板,第一印象可以理解为向下;

  • floor函数的意思就是向下取整,如果原先的数值为x,那么它返回不大于x的最大整数,即返回的整数≤x。

测试代码:

​​​​​​​

 package test_2024;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @ClassName Test2024010701
 * @Description TODO
 * @Author Jiangnan Cui
 * @Date 2024/1/7 17:19
 * @Version 1.0
 */
public class Test2024010701 {
    public static void main(String[] args) {
        // 1.正数
        double floor1 = Math.floor(3.0);
        System.out.println("floor1 = " + floor1);
        // floor1 = 3.0

        double floor2 = Math.floor(3.1);
        System.out.println("floor2 = " + floor2);
        // floor2 = 3.0

        double floor3 = Math.floor(3.4);
        System.out.println("floor3 = " + floor3);
        // floor3 = 3.0

        double floor4 = Math.floor(3.5);
        System.out.println("floor4 = " + floor4);
        // floor4 = 3.0

        double floor5 = Math.floor(3.7);
        System.out.println("floor5 = " + floor5);
        // floor5 = 3.0

        // 2.负数
        double floor11 = Math.floor(-3.0);
        System.out.println("floor11 = " + floor11);
        // floor11 = -3.0

        double floor21 = Math.floor(-3.1);
        System.out.println("floor21 = " + floor21);
        // floor21 = -4.0

        double floor31 = Math.floor(-3.4);
        System.out.println("floor31 = " + floor31);
        // floor31 = -4.0

        double floor41 = Math.floor(-3.5);
        System.out.println("floor41 = " + floor41);
        // floor41 = -4.0

        double floor51 = Math.floor(-3.7);
        System.out.println("floor51 = " + floor51);
        // floor51 = -4.0
    }
}

1.1.2 Math.ceil()向上取整

功能说明:
  • ceil这个单词用作名词可以表示为天花板,第一印象可以理解为向上;

  • ceil函数的意思就是向上取整,如果原先的数值为x,那么它返回不小于x的最大整数,即返回的整数≥x。

测试代码
package test_2024;

/**
 * @ClassName Test2024010702
 * @Description TODO
 * @Author Jiangnan Cui
 * @Date 2024/1/7 17:22
 * @Version 1.0
 */
public class Test2024010702 {
    public static void main(String[] args) {
        // 1.正数
        double ceil1 = Math.ceil(3.0);
        System.out.println("ceil1 = " + ceil1);
        // ceil1 = 3.0

        double ceil2 = Math.ceil(3.1);
        System.out.println("ceil2 = " + ceil2);
        // ceil2 = 4.0

        double ceil3 = Math.ceil(3.4);
        System.out.println("ceil3 = " + ceil3);
        // ceil3 = 4.0

        double ceil4 = Math.ceil(3.5);
        System.out.println("ceil4 = " + ceil4);
        // ceil4 = 4.0

        double ceil5 = Math.ceil(3.7);
        System.out.println("ceil5 = " + ceil5);
        // ceil5 = 4.0

        // 2.负数
        double ceil11 = Math.ceil(-3.0);
        System.out.println("ceil11 = " + ceil11);
        // ceil11 = -3.0

        double ceil21 = Math.ceil(-3.1);
        System.out.println("ceil21 = " + ceil21);
        // ceil21 = -3.0

        double ceil31 = Math.ceil(-3.4);
        System.out.println("ceil31 = " + ceil31);
        // ceil31 = -3.0

        double ceil41 = Math.ceil(-3.5);
        System.out.println("ceil41 = " + ceil41);
        // ceil41 = -3.0

        double ceil51 = Math.ceil(-3.7);
        System.out.println("ceil51 = " + ceil51);
        // ceil51 = -3.0
    }
}

1.2 double保留x位小数

1.2.1 保留1位

测试代码:
package test_2024;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @ClassName Test2024010703
 * @Description TODO
 * @Author Jiangnan Cui
 * @Date 2024/1/7 17:24
 * @Version 1.0
 */
public class Test2024010703 {
    public static void main(String[] args) {
        double decimal = 3.1415926;

        // 1. 使用BigDecimal的setScale方法
        BigDecimal bigDecimal = new BigDecimal(decimal);
        double bigDecimalDouble = bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println("bigDecimalDouble = " + bigDecimalDouble);
        // bigDecimalDouble = 3.1

        // 2. 使用DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        String formatDecimalString = decimalFormat.format(decimal);
        double doubleDecimal = Double.parseDouble(formatDecimalString);
        System.out.println("doubleDecimal = " + doubleDecimal);
        // doubleDecimal = 3.1

        // 3. 使用String自带的format方法
        String stringFormat = String.format("%.1f", decimal);
        // String stringFormat = StrUtil.format("%.1f", decimal);
        // 注意此处不能用StrUtil.format,因为上面输出结果是%.1f,会产生NumberFormatException
        double stringFormatDouble = Double.parseDouble(stringFormat);
        System.out.println("stringFormatDouble = " + stringFormatDouble);
        // stringFormatDouble = 3.1


        // 4. 使用NumberFormat设置最大小数位数
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(1);
        String numberFormatString = numberFormat.format(decimal);
        double numberFormatDouble = Double.parseDouble(numberFormatString);
        System.out.println("numberFormatDouble = " + numberFormatDouble);
        // numberFormatDouble = 3.1
        // 或
        NumberFormat numberFormat11 = NumberFormat.getNumberInstance();
        numberFormat11.setMaximumFractionDigits(1);
        String numberFormat11String = numberFormat11.format(decimal);
        double numberFormat11Double = Double.parseDouble(numberFormat11String);
        System.out.println("numberFormat11Double = " + numberFormat11Double);
        // numberFormat11Double = 3.1
    }
}

1.2.2 保留2位

测试代码:
package test_2024;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @ClassName Test2024070704
 * @Description TODO
 * @Author Jiangnan Cui
 * @Date 2024/1/7 17:26
 * @Version 1.0
 */
public class Test2024070704 {
    public static void main(String[] args) {
        double decimal = 3.1415926;

        // 1. 使用BigDecimal的setScale方法
        BigDecimal bigDecimal2 = new BigDecimal(decimal);
        double bigDecimal2Double = bigDecimal2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println("bigDecimal2Double = " + bigDecimal2Double);
        // bigDecimal2Double = 3.14

        // 2. 使用DecimalFormat
        DecimalFormat decimalFormat2 = new DecimalFormat("#.00");
        String decimalFormat2String = decimalFormat2.format(decimal);
        double decimalFormat2Double = Double.parseDouble(decimalFormat2String);
        System.out.println("decimalFormat2Double = " + decimalFormat2Double);
        // decimalFormat2Double = 3.14

        // 3. 使用Sting自带的format方法
        String stringFormat2 = String.format("%.2f", decimal);
        // String stringFormat2 = StrUtil.format("%.2f", decimal);
        // 注意此处不能用StrUtil.format,因为上面输出结果是%.2f,会产生NumberFormatException
        double stringFormat2Double = Double.parseDouble(stringFormat2);
        System.out.println("stringFormat2Double = " + stringFormat2Double);
        // stringFormat2Double = 3.14

        // 4. 使用NumberFormat设置最大小数位数
        NumberFormat numberFormat2 = NumberFormat.getInstance();
        numberFormat2.setMaximumFractionDigits(2);
        String numberFormat2String = numberFormat2.format(decimal);
        double numberFormat2Double = Double.parseDouble(numberFormat2String);
        System.out.println("numberFormat2Double = " + numberFormat2Double);
        // numberFormat2Double = 3.14
        // 或
        NumberFormat numberFormat22 = NumberFormat.getNumberInstance();
        numberFormat22.setMaximumFractionDigits(2);
        String numberFormat22String = numberFormat22.format(decimal);
        double numberFormat22Double = Double.parseDouble(numberFormat22String);
        System.out.println("numberFormat22Double = " + numberFormat22Double);
        // numberFormat22Double = 3.14
    }
}

其余内容后续待完善...

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值