史上最全BigDecimal的5种进位方式:ROUND_UP,ROUND_DOWN,ROUND_CEILING,ROUND_FLOOR,ROUND_HALF_UP,ROUND_HALF_DOWN的比较

先上JAVA官方文档

    /**
     * Rounding mode to round away from zero.  Always increments the
     * digit prior to a nonzero discarded fraction.  Note that this rounding
     * mode never decreases the magnitude of the calculated value.
     */
    public final static int ROUND_UP =           0;

    /**
     * Rounding mode to round towards zero.  Never increments the digit
     * prior to a discarded fraction (i.e., truncates).  Note that this
     * rounding mode never increases the magnitude of the calculated value.
     */
    public final static int ROUND_DOWN =         1;

    /**
     * Rounding mode to round towards positive infinity.  If the
     * {@code BigDecimal} is positive, behaves as for
     * {@code ROUND_UP}; if negative, behaves as for
     * {@code ROUND_DOWN}.  Note that this rounding mode never
     * decreases the calculated value.
     */
    public final static int ROUND_CEILING =      2;

    /**
     * Rounding mode to round towards negative infinity.  If the
     * {@code BigDecimal} is positive, behave as for
     * {@code ROUND_DOWN}; if negative, behave as for
     * {@code ROUND_UP}.  Note that this rounding mode never
     * increases the calculated value.
     */
    public final static int ROUND_FLOOR =        3;

    /**
     * Rounding mode to round towards {@literal "nearest neighbor"}
     * unless both neighbors are equidistant, in which case round up.
     * Behaves as for {@code ROUND_UP} if the discarded fraction is
     * ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}.  Note
     * that this is the rounding mode that most of us were taught in
     * grade school.
     */
    public final static int ROUND_HALF_UP =      4;

    /**
     * Rounding mode to round towards {@literal "nearest neighbor"}
     * unless both neighbors are equidistant, in which case round
     * down.  Behaves as for {@code ROUND_UP} if the discarded
     * fraction is {@literal >} 0.5; otherwise, behaves as for
     * {@code ROUND_DOWN}.
     */
    public final static int ROUND_HALF_DOWN =    5;

看不懂别急,看翻译版本

/**

*舍入模式,从零开始舍入。总是增加

*非零舍弃分数之前的数字。请注意,此舍入

*模式从不降低计算值的大小。

*/

public final static int ROUND_UP =           0;



/**

*舍入模式,向零舍入。从不递增数字

*在丢弃的分数之前(即截断)。请注意

*舍入模式从不增加计算值的大小。

*/

public final static int ROUND_DOWN =         1;



/**

*舍入模式,向正无穷大舍入。如果

*{@code BigDecimal}为正,行为与

*{@code ROUND\u};如果为负,则行为与

*{@code ROUND\u DOWN}。请注意,此舍入模式从不

*减小计算值。

*/

public final static int ROUND_CEILING =      2;



/**

*舍入模式,向负无穷大方向舍入。如果

*{@code BigDecimal}为正,行为与

*{@code ROUND\u DOWN};如果为负,则按

*{@code ROUND\u}。请注意,此舍入模式从不

*增加计算值。

*/

public final static int ROUND_FLOOR =        3;



/**

*舍入模式向{@literal“nearest neighbor”舍入

*除非两个邻居都是等距的,在这种情况下,就把他们围起来。

*如果丢弃的分数是

*≥0.5;否则,行为与{@code ROUND\u DOWN}相同。注意

*这就是我们大多数人所学的四舍五入模式

*小学。

*/

public final static int ROUND_HALF_UP =      4;


/**

*舍入模式向{@literal“nearest neighbor”舍入

*除非两个邻居是等距的,在这种情况下是圆的

*趴下。如果丢弃

*分数为{@literal>}0.5;否则,其行为与

*{@code ROUND\u DOWN}。

*/

public final static int ROUND_HALF_DOWN =    5;

看到这里,除了最后两个翻译的,不懂之外,其他的,应该都清楚了,

这里解释一下最后两个的差别:

ROUND_HALF_UP: 遇到.5的情况时往上近似,例: 1.5 ->;2
ROUND_HALF_DOWN : 遇到.5的情况时往下近似,例: 1.5 ->;1

仅此而已

 

收工

 

 

好的,以下是我对您的问题的回答: 1、键盘获得一个商品的全部信息,格式”编号:商品名称:价格”,通过方法将其分割成字符串数组并输出。 ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String[] infos = input.split(":"); for (String info : infos) { System.out.println(info); } } } ``` 2、将分割出的商品信息存入商品对象中。(注意类型要匹配)商品数量采用Random类随机生成。(1000-2000) ``` import java.math.BigDecimal; import java.util.Random; public class Main { public static void main(String[] args) { String input = "1001:apple:5.99"; String[] infos = input.split(":"); int productId = Integer.parseInt(infos[0]); String productName = infos[1]; BigDecimal price = new BigDecimal(infos[2]); int quantity = new Random().nextInt(1001) + 1000; Product product = new Product(productId, productName, price, quantity); } } class Product { private int productId; private String productName; private BigDecimal price; private int quantity; public Product(int productId, String productName, BigDecimal price, int quantity) { this.productId = productId; this.productName = productName; this.price = price; this.quantity = quantity; } } ``` 3、将10个商品对象存入商品数组中,输出商品数组中的内容。(显示具体内容而不是地址) ``` import java.math.BigDecimal; import java.util.Random; public class Main { public static void main(String[] args) { Product[] products = new Product[10]; for (int i = 0; i < 10; i++) { String input = "1001:apple:5.99"; String[] infos = input.split(":"); int productId = Integer.parseInt(infos[0]); String productName = infos[1]; BigDecimal price = new BigDecimal(infos[2]); int quantity = new Random().nextInt(1001) + 1000; products[i] = new Product(productId, productName, price, quantity); } for (Product product : products) { System.out.println(product.toString()); } } } class Product { private int productId; private String productName; private BigDecimal price; private int quantity; public Product(int productId, String productName, BigDecimal price, int quantity) { this.productId = productId; this.productName = productName; this.price = price; this.quantity = quantity; } @Override public String toString() { return "Product{" + "productId=" + productId + ", productName='" + productName + '\'' + ", price=" + price + ", quantity=" + quantity + '}'; } } ``` 4、使用BigDecimal计算出每商品的总价格和所有商品的总价格。(保留精度2位,进位模式为四舍五入) ``` import java.math.BigDecimal; import java.util.Random; public class Main { public static void main(String[] args) { Product[] products = new Product[10]; BigDecimal total = new BigDecimal(0); for (int i = 0; i < 10; i++) { String input = "1001:apple:5.99";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学编程的司马光

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

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

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

打赏作者

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

抵扣说明:

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

余额充值