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

时间:2025-02-18 07:50:18

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

1.1 double取整

1.1.1 ()向下取整

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

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

测试代码:

​​​​​​​

 package test_2024;

import ;
import ;
import ;

/**
 * @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 = (3.0);
        ("floor1 = " + floor1);
        // floor1 = 3.0

        double floor2 = (3.1);
        ("floor2 = " + floor2);
        // floor2 = 3.0

        double floor3 = (3.4);
        ("floor3 = " + floor3);
        // floor3 = 3.0

        double floor4 = (3.5);
        ("floor4 = " + floor4);
        // floor4 = 3.0

        double floor5 = (3.7);
        ("floor5 = " + floor5);
        // floor5 = 3.0

        // 2.负数
        double floor11 = (-3.0);
        ("floor11 = " + floor11);
        // floor11 = -3.0

        double floor21 = (-3.1);
        ("floor21 = " + floor21);
        // floor21 = -4.0

        double floor31 = (-3.4);
        ("floor31 = " + floor31);
        // floor31 = -4.0

        double floor41 = (-3.5);
        ("floor41 = " + floor41);
        // floor41 = -4.0

        double floor51 = (-3.7);
        ("floor51 = " + floor51);
        // floor51 = -4.0
    }
}

1.1.2 ()向上取整

功能说明:
  • 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 = (3.0);
        ("ceil1 = " + ceil1);
        // ceil1 = 3.0

        double ceil2 = (3.1);
        ("ceil2 = " + ceil2);
        // ceil2 = 4.0

        double ceil3 = (3.4);
        ("ceil3 = " + ceil3);
        // ceil3 = 4.0

        double ceil4 = (3.5);
        ("ceil4 = " + ceil4);
        // ceil4 = 4.0

        double ceil5 = (3.7);
        ("ceil5 = " + ceil5);
        // ceil5 = 4.0

        // 2.负数
        double ceil11 = (-3.0);
        ("ceil11 = " + ceil11);
        // ceil11 = -3.0

        double ceil21 = (-3.1);
        ("ceil21 = " + ceil21);
        // ceil21 = -3.0

        double ceil31 = (-3.4);
        ("ceil31 = " + ceil31);
        // ceil31 = -3.0

        double ceil41 = (-3.5);
        ("ceil41 = " + ceil41);
        // ceil41 = -3.0

        double ceil51 = (-3.7);
        ("ceil51 = " + ceil51);
        // ceil51 = -3.0
    }
}

1.2 double保留x位小数

1.2.1 保留1位

测试代码:
package test_2024;

import ;
import ;
import ;

/**
 * @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 = (1, BigDecimal.ROUND_HALF_UP).doubleValue();
        ("bigDecimalDouble = " + bigDecimalDouble);
        // bigDecimalDouble = 3.1

        // 2. 使用DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        String formatDecimalString = (decimal);
        double doubleDecimal = (formatDecimalString);
        ("doubleDecimal = " + doubleDecimal);
        // doubleDecimal = 3.1

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


        // 4. 使用NumberFormat设置最大小数位数
        NumberFormat numberFormat = ();
        (1);
        String numberFormatString = (decimal);
        double numberFormatDouble = (numberFormatString);
        ("numberFormatDouble = " + numberFormatDouble);
        // numberFormatDouble = 3.1
        // 或
        NumberFormat numberFormat11 = ();
        (1);
        String numberFormat11String = (decimal);
        double numberFormat11Double = (numberFormat11String);
        ("numberFormat11Double = " + numberFormat11Double);
        // numberFormat11Double = 3.1
    }
}

1.2.2 保留2位

测试代码:
package test_2024;

import ;
import ;
import ;

/**
 * @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 = (2, BigDecimal.ROUND_HALF_UP).doubleValue();
        ("bigDecimal2Double = " + bigDecimal2Double);
        // bigDecimal2Double = 3.14

        // 2. 使用DecimalFormat
        DecimalFormat decimalFormat2 = new DecimalFormat("#.00");
        String decimalFormat2String = (decimal);
        double decimalFormat2Double = (decimalFormat2String);
        ("decimalFormat2Double = " + decimalFormat2Double);
        // decimalFormat2Double = 3.14

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

        // 4. 使用NumberFormat设置最大小数位数
        NumberFormat numberFormat2 = ();
        (2);
        String numberFormat2String = (decimal);
        double numberFormat2Double = (numberFormat2String);
        ("numberFormat2Double = " + numberFormat2Double);
        // numberFormat2Double = 3.14
        // 或
        NumberFormat numberFormat22 = ();
        (2);
        String numberFormat22String = (decimal);
        double numberFormat22Double = (numberFormat22String);
        ("numberFormat22Double = " + numberFormat22Double);
        // numberFormat22Double = 3.14
    }
}

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