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


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

    1.1 double取整

    1.1.1 Math.floor()向下取整

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

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

    测试代码:

    ​​​​​​​

    1. package test_2024;
    2. import java.math.BigDecimal;
    3. import java.text.DecimalFormat;
    4. import java.text.NumberFormat;
    5. /**
    6. * @ClassName Test2024010701
    7. * @Description TODO
    8. * @Author Jiangnan Cui
    9. * @Date 2024/1/7 17:19
    10. * @Version 1.0
    11. */
    12. public class Test2024010701 {
    13. public static void main(String[] args) {
    14. // 1.正数
    15. double floor1 = Math.floor(3.0);
    16. System.out.println("floor1 = " + floor1);
    17. // floor1 = 3.0
    18. double floor2 = Math.floor(3.1);
    19. System.out.println("floor2 = " + floor2);
    20. // floor2 = 3.0
    21. double floor3 = Math.floor(3.4);
    22. System.out.println("floor3 = " + floor3);
    23. // floor3 = 3.0
    24. double floor4 = Math.floor(3.5);
    25. System.out.println("floor4 = " + floor4);
    26. // floor4 = 3.0
    27. double floor5 = Math.floor(3.7);
    28. System.out.println("floor5 = " + floor5);
    29. // floor5 = 3.0
    30. // 2.负数
    31. double floor11 = Math.floor(-3.0);
    32. System.out.println("floor11 = " + floor11);
    33. // floor11 = -3.0
    34. double floor21 = Math.floor(-3.1);
    35. System.out.println("floor21 = " + floor21);
    36. // floor21 = -4.0
    37. double floor31 = Math.floor(-3.4);
    38. System.out.println("floor31 = " + floor31);
    39. // floor31 = -4.0
    40. double floor41 = Math.floor(-3.5);
    41. System.out.println("floor41 = " + floor41);
    42. // floor41 = -4.0
    43. double floor51 = Math.floor(-3.7);
    44. System.out.println("floor51 = " + floor51);
    45. // floor51 = -4.0
    46. }
    47. }

    1.1.2 Math.ceil()向上取整

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

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

    测试代码
    1. package test_2024;
    2. /**
    3. * @ClassName Test2024010702
    4. * @Description TODO
    5. * @Author Jiangnan Cui
    6. * @Date 2024/1/7 17:22
    7. * @Version 1.0
    8. */
    9. public class Test2024010702 {
    10. public static void main(String[] args) {
    11. // 1.正数
    12. double ceil1 = Math.ceil(3.0);
    13. System.out.println("ceil1 = " + ceil1);
    14. // ceil1 = 3.0
    15. double ceil2 = Math.ceil(3.1);
    16. System.out.println("ceil2 = " + ceil2);
    17. // ceil2 = 4.0
    18. double ceil3 = Math.ceil(3.4);
    19. System.out.println("ceil3 = " + ceil3);
    20. // ceil3 = 4.0
    21. double ceil4 = Math.ceil(3.5);
    22. System.out.println("ceil4 = " + ceil4);
    23. // ceil4 = 4.0
    24. double ceil5 = Math.ceil(3.7);
    25. System.out.println("ceil5 = " + ceil5);
    26. // ceil5 = 4.0
    27. // 2.负数
    28. double ceil11 = Math.ceil(-3.0);
    29. System.out.println("ceil11 = " + ceil11);
    30. // ceil11 = -3.0
    31. double ceil21 = Math.ceil(-3.1);
    32. System.out.println("ceil21 = " + ceil21);
    33. // ceil21 = -3.0
    34. double ceil31 = Math.ceil(-3.4);
    35. System.out.println("ceil31 = " + ceil31);
    36. // ceil31 = -3.0
    37. double ceil41 = Math.ceil(-3.5);
    38. System.out.println("ceil41 = " + ceil41);
    39. // ceil41 = -3.0
    40. double ceil51 = Math.ceil(-3.7);
    41. System.out.println("ceil51 = " + ceil51);
    42. // ceil51 = -3.0
    43. }
    44. }

    1.2 double保留x位小数

    1.2.1 保留1位

    测试代码:
    1. package test_2024;
    2. import java.math.BigDecimal;
    3. import java.text.DecimalFormat;
    4. import java.text.NumberFormat;
    5. /**
    6. * @ClassName Test2024010703
    7. * @Description TODO
    8. * @Author Jiangnan Cui
    9. * @Date 2024/1/7 17:24
    10. * @Version 1.0
    11. */
    12. public class Test2024010703 {
    13. public static void main(String[] args) {
    14. double decimal = 3.1415926;
    15. // 1. 使用BigDecimal的setScale方法
    16. BigDecimal bigDecimal = new BigDecimal(decimal);
    17. double bigDecimalDouble = bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
    18. System.out.println("bigDecimalDouble = " + bigDecimalDouble);
    19. // bigDecimalDouble = 3.1
    20. // 2. 使用DecimalFormat
    21. DecimalFormat decimalFormat = new DecimalFormat("#.0");
    22. String formatDecimalString = decimalFormat.format(decimal);
    23. double doubleDecimal = Double.parseDouble(formatDecimalString);
    24. System.out.println("doubleDecimal = " + doubleDecimal);
    25. // doubleDecimal = 3.1
    26. // 3. 使用String自带的format方法
    27. String stringFormat = String.format("%.1f", decimal);
    28. // String stringFormat = StrUtil.format("%.1f", decimal);
    29. // 注意此处不能用StrUtil.format,因为上面输出结果是%.1f,会产生NumberFormatException
    30. double stringFormatDouble = Double.parseDouble(stringFormat);
    31. System.out.println("stringFormatDouble = " + stringFormatDouble);
    32. // stringFormatDouble = 3.1
    33. // 4. 使用NumberFormat设置最大小数位数
    34. NumberFormat numberFormat = NumberFormat.getInstance();
    35. numberFormat.setMaximumFractionDigits(1);
    36. String numberFormatString = numberFormat.format(decimal);
    37. double numberFormatDouble = Double.parseDouble(numberFormatString);
    38. System.out.println("numberFormatDouble = " + numberFormatDouble);
    39. // numberFormatDouble = 3.1
    40. // 或
    41. NumberFormat numberFormat11 = NumberFormat.getNumberInstance();
    42. numberFormat11.setMaximumFractionDigits(1);
    43. String numberFormat11String = numberFormat11.format(decimal);
    44. double numberFormat11Double = Double.parseDouble(numberFormat11String);
    45. System.out.println("numberFormat11Double = " + numberFormat11Double);
    46. // numberFormat11Double = 3.1
    47. }
    48. }

    1.2.2 保留2位

    测试代码:
    1. package test_2024;
    2. import java.math.BigDecimal;
    3. import java.text.DecimalFormat;
    4. import java.text.NumberFormat;
    5. /**
    6. * @ClassName Test2024070704
    7. * @Description TODO
    8. * @Author Jiangnan Cui
    9. * @Date 2024/1/7 17:26
    10. * @Version 1.0
    11. */
    12. public class Test2024070704 {
    13. public static void main(String[] args) {
    14. double decimal = 3.1415926;
    15. // 1. 使用BigDecimal的setScale方法
    16. BigDecimal bigDecimal2 = new BigDecimal(decimal);
    17. double bigDecimal2Double = bigDecimal2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    18. System.out.println("bigDecimal2Double = " + bigDecimal2Double);
    19. // bigDecimal2Double = 3.14
    20. // 2. 使用DecimalFormat
    21. DecimalFormat decimalFormat2 = new DecimalFormat("#.00");
    22. String decimalFormat2String = decimalFormat2.format(decimal);
    23. double decimalFormat2Double = Double.parseDouble(decimalFormat2String);
    24. System.out.println("decimalFormat2Double = " + decimalFormat2Double);
    25. // decimalFormat2Double = 3.14
    26. // 3. 使用Sting自带的format方法
    27. String stringFormat2 = String.format("%.2f", decimal);
    28. // String stringFormat2 = StrUtil.format("%.2f", decimal);
    29. // 注意此处不能用StrUtil.format,因为上面输出结果是%.2f,会产生NumberFormatException
    30. double stringFormat2Double = Double.parseDouble(stringFormat2);
    31. System.out.println("stringFormat2Double = " + stringFormat2Double);
    32. // stringFormat2Double = 3.14
    33. // 4. 使用NumberFormat设置最大小数位数
    34. NumberFormat numberFormat2 = NumberFormat.getInstance();
    35. numberFormat2.setMaximumFractionDigits(2);
    36. String numberFormat2String = numberFormat2.format(decimal);
    37. double numberFormat2Double = Double.parseDouble(numberFormat2String);
    38. System.out.println("numberFormat2Double = " + numberFormat2Double);
    39. // numberFormat2Double = 3.14
    40. // 或
    41. NumberFormat numberFormat22 = NumberFormat.getNumberInstance();
    42. numberFormat22.setMaximumFractionDigits(2);
    43. String numberFormat22String = numberFormat22.format(decimal);
    44. double numberFormat22Double = Double.parseDouble(numberFormat22String);
    45. System.out.println("numberFormat22Double = " + numberFormat22Double);
    46. // numberFormat22Double = 3.14
    47. }
    48. }

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

  • 相关阅读:
    GO 集合 map 使用总结
    Activiti源码跟踪之模型Model操作
    【AIGC】大语言模型
    ICLR‘23 UnderReview | LightGCL: 简单而有效的图对比学习推荐系统
    [go学习笔记.第十一章.项目案例] 2.客户信息管理系统
    Maven插件开发
    01 邂逅typescript,环境搭建
    MySQL何时适合创建索引,需要注意什么以及创建原则
    自动化测试有必要学吗?一篇从功能测试进阶到自动化测试...
    基于知识图谱建模、全文检索的智能知识管理库(源码)
  • 原文地址:https://blog.csdn.net/xiaocui1995/article/details/127939295