java怎样解决除法精度_java 除法 精度问题

int a=4;

int b=3;

float c = (float) a/b;

System.out.print(c);//输出:1

如果要的到精确的结果,要用下面的方法

int a=4;

int b=3;

float c = (float) a/(float) b;

System.out.print(c);//输出:1.3333334

import java.text.DecimalFormat;

public class toDouble {

public static void main(String[] args){

//增加三位小数0

DecimalFormat df = new DecimalFormat("0.000");

double d = 123;

System.out.println(df.format(d));

//保留4位小数

double d1 = 123.0123456;

String d2 = String.format("%.4f", d1);

System.out.println(d2);

int d3=5;

//float d4=d3/100;

String d4 = String.format("%.3f", (float)d3/100.00);

System.out.println(d4);

}

}

输出:

123.000

123.0123

0.050

Java除法保留3位小数的几种方法

import java.math.BigDecimal;

import java.text.DecimalFormat;

import java.text.NumberFormat;

public class format {

double f = 0.5585;

double f1 = 11.5585;

public void m1() {

//数字

BigDecimal bg = new BigDecimal(f);

double f1 = bg.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();

System.out.println(f1);

}

/**

* DecimalFormat转换最简便

*/

public void m2() {

//字符串

DecimalFormat df = new DecimalFormat("0.000");//对于大于1的用"#.000",小于1的要用"0.000"

String t=df.format(f);

System.out.println(t);

DecimalFormat df1 = new DecimalFormat("#.000");

System.out.println(df1.format(f1));

}

/**

* String.format打印最简便

*/

public void m3() {

//字符串

String t =String.format("%.3f", f);

System.out.println(t);

}

public void m4() {

//字符串

NumberFormat nf = NumberFormat.getNumberInstance();

nf.setMaximumFractionDigits(3);

String t =nf.format(f);

System.out.println(t);

}

public static void main(String[] args) {

format f = new format();

f.m1();

f.m2();

f.m3();

f.m4();

}

}

结果:

0.558

0.558

11.558

0.559

0.558

Math.ceilMath.roundMath.floor

floor 向下取整 ceil  向上取整 round 则是4舍5入的计算,round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。 Math.floor(1.4)=1.0 Math.round(1.4)=1 Math.ceil(1.4)=2.0 Math.floor(1.5)=1.0 Math.round(1.5)=2 Math.ceil(1.5)=2.0 Math.floor(1.6)=1.0 Math.round(1.6)=2 Math.ceil(1.6)=2.0 Math.floor(-1.4)=-2.0 Math.round(-1.4)=-1 Math.ceil(-1.4)=-1.0 Math.floor(-1.5)=-2.0 Math.round(-1.5)=-1 Math.ceil(-1.5)=-1.0 Math.floor(-1.6)=-2.0 Math.round(-1.6)=-2 Math.ceil(-1.6)=-1.0

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
唔,其实里面就是一个工具类,加减乘除、保留两位小数。一共5个方法。。。emmmm.....为啥分这么高呢。因为宝宝想分想疯了。 附代码,有土豪就打赏打赏,没土豪的直接拿去使吧。 package cn.cisdom.base.utils; import java.math.BigDecimal; import java.text.DecimalFormat; public class Calculation { public static final DecimalFormat df = new DecimalFormat("######0.00"); /** * @methodName format2point * @desc 保留两位小数点 * @param value * @return java.lang.String * @author xm * @create 2018/6/7 12:03 **/ public static String format2point(Number value) { return df.format(value); } public static Double add(Number value1, Number value2) { BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue())); BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue())); return b1.add(b2).doubleValue(); } /** * 提供精确的减法运算。 * * @param value1 * 减数 * @param value2 * 被减数 * @return 两个参数的差 */ public static Double sub(Number value1, Number value2) { BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue())); BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue())); return b1.subtract(b2).doubleValue(); } /** * 提供精确的乘法运算。 * * @param value1 * 被乘数 * @param value2 * 乘数 * @return 两个参数的积 */ public static Double mul(Number value1, Number value2) { BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue())); BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue())); return b1.multiply(b2).doubleValue(); } /** * 提供精确的除法运算。 * * @param value1 * 除数 * @param value2 * 被除数 * @return 除数/被除数 */ public static Double div(Number value1, Number value2) { //MathContext mc = new MathContext(2, RoundingMode.HALF_DOWN);//精度为2,舍入模式为大于0.5进1,否则舍弃 BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue())); BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue())); return b1.divide(b2).doubleValue(); } public static void main(String[] args) { Double aDouble=Calculation.add(56.9, 1.67); System.out.println(aDouble); Double bDouble=Calculation.sub(99.2,aDouble); System.out.println(bDouble); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值