import java. math. BigDecimal ;
import java. math. RoundingMode ;
public class BigDecimalUtil { private static final int DEF_DIV_SCALE = 10 ; private BigDecimalUtil ( ) { } public static double add ( double v1, double v2) { BigDecimal b1 = BigDecimal . valueOf ( v1) ; BigDecimal b2 = BigDecimal . valueOf ( v2) ; return b1. add ( b2) . doubleValue ( ) ; } public static double subtract ( double v1, double v2) { BigDecimal b1 = BigDecimal . valueOf ( v1) ; BigDecimal b2 = BigDecimal . valueOf ( v2) ; return b1. subtract ( b2) . doubleValue ( ) ; } public static double multiply ( double v1, double v2) { BigDecimal b1 = BigDecimal . valueOf ( v1) ; BigDecimal b2 = BigDecimal . valueOf ( v2) ; return b1. multiply ( b2) . doubleValue ( ) ; } public static double divide ( double v1, double v2) { return divide ( v1, v2, DEF_DIV_SCALE ) ; } public static double divide ( double v1, double v2, int scale) { if ( scale < 0 ) { throw new IllegalArgumentException ( "The scale must be a positive integer or zero" ) ; } BigDecimal b1 = BigDecimal . valueOf ( v1) ; BigDecimal b2 = BigDecimal . valueOf ( v2) ; return b1. divide ( b2, scale, RoundingMode . HALF_UP ) . doubleValue ( ) ; } public static double round ( double v, int scale) { if ( scale < 0 ) { throw new IllegalArgumentException ( "The scale must be a positive integer or zero" ) ; } BigDecimal b = BigDecimal . valueOf ( v) ; BigDecimal one = new BigDecimal ( "1" ) ; return b. divide ( one, scale, RoundingMode . HALF_UP ) . doubleValue ( ) ; } public static float convertToFloat ( double v) { BigDecimal b = new BigDecimal ( v) ; return b. floatValue ( ) ; } public static int convertsToInt ( double v) { BigDecimal b = new BigDecimal ( v) ; return b. intValue ( ) ; } public static long convertsToLong ( double v) { BigDecimal b = new BigDecimal ( v) ; return b. longValue ( ) ; } public static double returnMax ( double v1, double v2) { BigDecimal b1 = new BigDecimal ( v1) ; BigDecimal b2 = new BigDecimal ( v2) ; return b1. max ( b2) . doubleValue ( ) ; } public static double returnMin ( double v1, double v2) { BigDecimal b1 = new BigDecimal ( v1) ; BigDecimal b2 = new BigDecimal ( v2) ; return b1. min ( b2) . doubleValue ( ) ; } public static int compareTo ( double v1, double v2) { BigDecimal b1 = BigDecimal . valueOf ( v1) ; BigDecimal b2 = BigDecimal . valueOf ( v2) ; return b1. compareTo ( b2) ; } }