float保留小数位数

package com.bonree.browser.util;

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

import static javafx.scene.input.KeyCode.L;
import static javafx.scene.input.KeyCode.M;

/*********************************************************************
* 版权信息:博睿宏远科技发展有限公司
* Copyright: Copyright (c) 2007博睿宏远科技发展有限公司,Inc.All Rights Reserved.
* Description: java中保留两位小数的方法
* Created by bonree on 2018/2/5.
********************************************************************/
public class FloatUtils {
public static void main(String[] args) {
/**
//方法一:
float f = 134.3435f;
int scale = 2;//设置尾数
int roundingMode = 4;//表示四舍五入,可以选择其他的舍值方式,例如去位等等
BigDecimal b = new BigDecimal(f);
b = b.setScale(scale, roundingMode);
System.out.printf(String.valueOf(b.floatValue()));
**/

    /****
     //方法二:
     float scale = (float) 34.236323;
     DecimalFormat format = new DecimalFormat("##0.00");
     String dd = format.format(scale);
     System.out.printf(dd);
     ****/
    /***
     // 方法三:
     float a = 12.32334f;
     float b = Math.round(a * 100) / 100;
     ***/

    /****
     //方法四:
     float a = 123.2334f;
     String b = String.format("%.2f", a);
     System.out.printf(b);
     ****/
    /***
     //方法5
     float a = 123.2334f;
     NumberFormat nf = NumberFormat.getNumberInstance();
     nf.setMaximumFractionDigits(2);
     System.out.printf(nf.format(a));
     **/

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值