Several usages of Java--BigDecimal's Setscale

Source: Internet
Author: User

Setscale the precision of the numerical value of the BigDecimal type.

Public BigDecimal setscale (int newscale, int roundingmode) {
    ...
}
Newscale retains the number of bigdecimal digits. Roundingmode has the following processing modes:
    Public final static int round_up =           0;

    Public final static int round_down =         1;

    Public final static int round_ceiling =      2;

    Public final static int round_floor =        3;

    Public final static int round_half_up =      4;

    Public final static int round_half_down =    5;

    Public final static int round_half_even =    6;

    Public final static int round_unnecessary =  7;

Interpret all by a main method. Go ...

Package com.redhorse.util;

Import Java.math.BigDecimal;
Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;  /** * To avoid loss of calculation precision, convert double type to BigDecimal for correlation calculation * * @author Kang */public class Bigdecimalutils {/** * Carry up * * @author: Gao Peng * @date: June 21, 2016 afternoon 3:06:55 * @param: @param * newscal E * @param: @param * b * @param: @return * @return: BigDecimal */public static B
    Igdecimal setscaleroundup (int newscale, BigDecimal b) {return B.setscale (Newscale, bigdecimal.round_up); /** * * @author: Gao Peng * @date: June 21, 2016 afternoon 5:18:41 * @param: @param * NE Wscale * @param: @param * b * @param: @return * @return: BigDecimal * * Public STA 
    Tic BigDecimal setscalerounddown (int newscale, BigDecimal b) {return B.setscale (Newscale, Bigdecimal.round_down); }/** * * @auThor:gao Peng * @date: June 21, 2016 afternoon 5:18:44 * @param: @param * newscale * @param: @param * b * @param: @return * @return: BigDecimal/public static BigDecimal SETSCALEROUNDC
    eiling (int newscale, BigDecimal b) {return B.setscale (Newscale, bigdecimal.round_ceiling); /** * * @author: Gao Peng * @date: June 21, 2016 afternoon 5:18:47 * @param: @param * NE Wscale * @param: @param * b * @param: @return * @return: BigDecimal * * Public STA Tic BigDecimal setscaleroundfloor (int newscale, BigDecimal b) {return B.setscale (Newscale, Bigdecimal.round_floor
    ); /** * * @author: Gao Peng * @date: June 21, 2016 afternoon 5:18:50 * @param: @param * NE Wscale * @param: @param * b * @param: @return * @return: BigDecimal * * Public STA Tic BigDecimal Setscaleroundhalfup (int Newscale, BigDecimal b) {return B.setscale (Newscale, bigdecimal.round_half_up); /** * * @author: Gao Peng * @date: June 21, 2016 afternoon 5:18:53 * @param: @param * NE Wscale * @param: @param * b * @param: @return * @return: BigDecimal * * Public STA Tic BigDecimal setscaleroundhalfdown (int newscale, BigDecimal b) {return B.setscale (Newscale, Bigdecimal.round_ha
    Lf_down); /** * * @author: Gao Peng * @date: June 21, 2016 afternoon 5:18:56 * @param: @param * NE Wscale * @param: @param * b * @param: @return * @return: BigDecimal * * Public STA Tic BigDecimal setscaleroundhalfeven (int newscale, BigDecimal b) {return B.setscale (Newscale, Bigdecimal.round_ha
    Lf_even); /** * * @author: Gao Peng * @date: June 21, 2016 afternoon 5:18:59 * @param: @param * NE Wscale * @param: @param * b * @param: @return * @return: BigDecimal/public static BigDecimal Setsca
    leroundunnecessary (int newscale, BigDecimal b) {return B.setscale (Newscale, bigdecimal.round_unnecessary);
        public static void Main (string[] args) {BigDecimal b = new BigDecimal ("1.251");
        System.out.println ("Execution value:" + b.tostring ());
        If B is a positive number, the roundup operation is performed, otherwise the RoundDown operation System.out.println ("setscaleroundceiling:" + setscaleroundceiling (1, b)); 
        If B is a positive number, the RoundDown operation is performed, otherwise the roundup operation System.out.println ("Setscaleroundfloor:" + setscaleroundfloor (1, b));
        Carry System.out.println ("Setscaleroundup:" + setscaleroundup (1, b));
        Truncated System.out.println ("Setscalerounddown:" + setscalerounddown (1, b));
        Rounding, intercepting decimal > 5 carry System.out.println ("Setscaleroundhalfdown:" + Setscaleroundhalfdown (2, b)); Rounding, intercepting decimal >=. 5 Carry System.out.println ("SETSCAleroundhalfup: "+ setscaleroundhalfup (2, b));
        If the left digit of the discarded part is odd, it is round_half_up, and if it is an even number, it is round_half_down. Odd/Even after the number of digits is discarded, the last judgment of the value, such as 4.123, retains 1 bits, 4.1, then 1 is the odd System.out.println ("Setscaleroundhalfeven:" + Setscaleroundhalfe
        Ven (1, b));
        The "pseudo-rounding mode" actually indicates that the required operation must be accurate, and therefore does not require a rounding operation.
    System.out.println ("setscaleroundunnecessary:" + setscaleroundunnecessary (3, b)); }
}

Here's one note that the BigDecimal provides a method in which you can pass in a value of type double, which is to be noted that the value of the double is not accurate.
For example BigDecimal B = new BigDecimal (1.251);
Output b:1.250999999999999889865875957184471189975738525390625

Reference
[http://esffor.iteye.com/blog/96157http://blog.csdn.net/wangjunjun2008/article/details/45559655
http://blog.csdn.net/lijunlinlijunlin/article/details/12218393

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.