如何将double或float类型的数字转换为int类型的数字,并选择所需要保留的小数点个数

本文介绍了Java中将double和float类型数值转换为int类型的三种方法:1) 强制类型转换,直接将浮点数转换为整数,会丢失小数部分;2) 使用Math.round()进行四舍五入转换;3) 结合DecimalFormat类保留指定小数位数。示例代码详细展示了转换过程。
摘要由CSDN通过智能技术生成

double/float转换为int类型

1.强制类型转换

package Test;

/**
 * @author Marston
 * @date 2021/9/28
 */
public class test1 {
    public static void main(String[] args) {
        double dou = 18.165985;
        float flo = 18.1612f;
        //将double/float转换为int —使用类型转换
        int douInt = (int)dou;
        int floInt = (int) flo;
         System.out.println("double:"+dou+",转换为的int:"+douInt);//double:18.165985,转换为的int:18
        System.out.println("float:"+flo+",转换为的int:"+floInt);//float:18.1612,转换为的int:18

        
    }
}

2.使用math函数,比如Math.round()

package Test;

/**
 * @author Marston
 * @date 2021/9/28
 */
public class test1 {
    public static void main(String[] args) {
        double dou = 18.165985;
        float flo = 18.1612f;
        //将double/float转换为int —使用类型转换
        int douInt = (int) Math.round(dou);//首先通过math函数为long类型,然后再强制转换为int类型
        int floInt = Math.round(flo);
        System.out.println("double:"+dou+",转换为的int:"+douInt);//double:18.165985,转换为的int:18
        System.out.println("float:"+flo+",转换为的int:"+floInt);//float:18.1612,转换为的int:18

    }
}

//将double转换为int —使用 Math.round(),四舍五入获取

选择要保留的小数点个数

四舍五入(只能保留一位小数)

package Test;

/**
 * @author Marston
 * @date 2021/9/28
 */
public class test1 {
    public static void main(String[] args) {
        double dou = 18.165985;
        float flo = 18.1612f;
        //四舍五入,先乘100,再除100
        double douInt = (double) (Math.round(dou*100)/100);
        double floInt = Math.round(flo*100)/100;
        System.out.println("double:"+dou+",转换为的double:"+douInt);//double:18.165985,转换为的double:18.0
        System.out.println("float:"+flo+",转换为的float:"+floInt);//float:18.1612,转换为的float:18.0

    }
}

使用DecimalFormat(转换为字符串)

保留两位

package Test;

import java.text.DecimalFormat;

/**
 * @author Marston
 * @date 2021/9/28
 */
public class test1 {
    public static void main(String[] args) {
        double dou = 18.165985;
        float flo = 18.1612f;
        //四舍五入,先乘100,再除100
        DecimalFormat df = new DecimalFormat("#.00");
        String f = df.format(dou);
        String f1 = df.format(flo);
        System.out.println("double:"+dou+",转换为保留多位小数double:"+f);//double:18.165985,转换为保留多位小数double:18.17
        System.out.println("float:"+flo+",转换为的float:"+f1);//float:18.1612,转换为的float:18.16

    }
}

保留2位+,依此类推

package Test;

import java.text.DecimalFormat;

/**
 * @author Marston
 * @date 2021/9/28
 */
public class test1 {
    public static void main(String[] args) {
        double dou = 18.165985;
        float flo = 18.1612f;
        //四舍五入,先乘100,再除100
        DecimalFormat df = new DecimalFormat("#.00000");
        String f = df.format(dou);
        String f1 = df.format(flo);
        System.out.println("double:"+dou+",转换为保留多位小数double:"+f);//double:18.165985,转换为保留多位小数double:18.16598
        System.out.println("float:"+flo+",转换为的float:"+f1);//float:18.1612,转换为的float:18.16120

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

?abc!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值