如何使用BigDecimal正确划分 [英] How can I divide properly using BigDecimal
本文介绍了如何使用BigDecimal正确划分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码示例:
import java.math.*;
public class x
{
public static void main(String[] args)
{
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c = a.divide(b, BigDecimal.ROUND_HALF_UP);
System.out.println(a+"/"+b+" = "+c);
}
}
结果是:
1/3 = 0
我做错了什么?
推荐答案
您没有为结果指定比例。请试试这个
You havent specified a scale for the result. Please try this
import java.math.*;
public class x
{
public static void main(String[] args)
{
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c = a.divide(b,2, BigDecimal.ROUND_HALF_UP);
System.out.println(a+"/"+b+" = "+c);
}
}
这将得到0.33的结果。请阅读 API
this will give the result as 0.33. Please read the API
这篇关于如何使用BigDecimal正确划分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文