public class MyUnits {
public static final double PT_PER_PX = 0.75 ;
public static final int IN_PER_PT = 72 ;
public static final double CM_PER_PT = 28.3 ;
public static final double MM_PER_PT = 2.83 ;
public static final int EMU_PER_PX = 9525 ;
public static final int pxToEMU ( double px) {
return DoubleUtils. mul ( px, EMU_PER_PX) . intValue ( ) ;
}
public static final int emuToPx ( double emu) {
return DoubleUtils. div ( emu, EMU_PER_PX) . intValue ( ) ;
}
public static final double ptToPx ( double pt) {
return DoubleUtils. div ( pt, PT_PER_PX) ;
}
public static final double inToPx ( double in) {
return DoubleUtils. mul ( inToPt ( in) , PT_PER_PX) ;
}
public static final double pxToIn ( double px) {
return ptToIn ( pxToPt ( px) ) ;
}
public static final double cmToPx ( double cm) {
return DoubleUtils. mul ( cmToPt ( cm) , PT_PER_PX) ;
}
public static final double pxToCm ( double px) {
return ptToCm ( pxToPt ( px) ) ;
}
public static final double mmToPx ( double mm) {
return DoubleUtils. mul ( mmToPt ( mm) , PT_PER_PX) ;
}
public static final double pxToMm ( double px) {
return ptToMm ( pxToPt ( px) ) ;
}
public static final double ptToIn ( double pt) {
return DoubleUtils. div ( pt, IN_PER_PT) ;
}
public static final double ptToMm ( double mm) {
return DoubleUtils. div ( mm, MM_PER_PT) ;
}
public static final double ptToCm ( double in) {
return DoubleUtils. div ( in, CM_PER_PT) ;
}
public static final double pxToPt ( double px) {
return DoubleUtils. mul ( px, PT_PER_PX) ;
}
public static final double inToPt ( double in) {
return DoubleUtils. mul ( in, IN_PER_PT) ;
}
public static final double mmToPt ( double mm) {
return DoubleUtils. mul ( mm, MM_PER_PT) ;
}
public static final double cmToPt ( double cm) {
return DoubleUtils. mul ( cm, CM_PER_PT) ;
}
}
public class DoubleUtils {
private static final Integer DEF_DIV_SCALE = 2 ;
public static Double add ( Number value1, Number value2) {
return add ( value1, value2, DEF_DIV_SCALE) ;
}
public static Double add ( Number value1, Number value2, Integer scale) {
BigDecimal b1 = new BigDecimal ( Double. toString ( value1. doubleValue ( ) ) ) ;
BigDecimal b2 = new BigDecimal ( Double. toString ( value2. doubleValue ( ) ) ) ;
BigDecimal add = b1. add ( b2) ;
BigDecimal setScale = add. setScale ( scale, BigDecimal. ROUND_HALF_UP) ;
return setScale. doubleValue ( ) ;
}
public static Double sub ( Number value1, Number value2) {
return sub ( value1, value2, DEF_DIV_SCALE) ;
}
public static Double sub ( Number value1, Number value2, Integer scale) {
BigDecimal b1 = new BigDecimal ( Double. toString ( value1. doubleValue ( ) ) ) ;
BigDecimal b2 = new BigDecimal ( Double. toString ( value2. doubleValue ( ) ) ) ;
BigDecimal subtract = b1. subtract ( b2) ;
BigDecimal setScale = subtract. setScale ( scale, BigDecimal. ROUND_HALF_UP) ;
return setScale. doubleValue ( ) ;
}
public static Double mul ( Number value1, Number value2) {
return mul ( value1, value2, DEF_DIV_SCALE) ;
}
public static Double mul ( Number value1, Number value2, Integer scale) {
BigDecimal b1 = new BigDecimal ( Double. toString ( value1. doubleValue ( ) ) ) ;
BigDecimal b2 = new BigDecimal ( Double. toString ( value2. doubleValue ( ) ) ) ;
BigDecimal multiply = b1. multiply ( b2) ;
BigDecimal setScale = multiply. setScale ( scale, BigDecimal. ROUND_HALF_UP) ;
return setScale. doubleValue ( ) ;
}
public static Double mul ( Number value1, Number value2, int scale, int roundingMode) {
BigDecimal b1 = new BigDecimal ( Double. toString ( value1. doubleValue ( ) ) ) ;
BigDecimal b2 = new BigDecimal ( Double. toString ( value2. doubleValue ( ) ) ) ;
BigDecimal multiply = b1. multiply ( b2) ;
BigDecimal setScale = multiply. setScale ( scale, roundingMode) ;
return setScale. doubleValue ( ) ;
}
public static Double div ( Number dividend, Number divisor) {
return DoubleUtils. div ( dividend, divisor, DEF_DIV_SCALE) ;
}
public static Double div ( Number dividend, Number divisor, Integer scale) {
if ( scale < 0 ) {
throw new IllegalArgumentException ( "The scale must be a positive integer or zero" ) ;
}
BigDecimal b1 = new BigDecimal ( Double. toString ( dividend. doubleValue ( ) ) ) ;
BigDecimal b2 = new BigDecimal ( Double. toString ( divisor. doubleValue ( ) ) ) ;
return b1. divide ( b2, scale, BigDecimal. ROUND_HALF_UP) . doubleValue ( ) ;
}
public static Double div ( Number dividend, Number divisor, Integer scale, int roundingMode) {
if ( scale < 0 ) {
throw new IllegalArgumentException ( "The scale must be a positive integer or zero" ) ;
}
BigDecimal b1 = new BigDecimal ( Double. toString ( dividend. doubleValue ( ) ) ) ;
BigDecimal b2 = new BigDecimal ( Double. toString ( divisor. doubleValue ( ) ) ) ;
return b1. divide ( b2, scale, roundingMode) . doubleValue ( ) ;
}
public static Double round ( Double value, Integer scale) {
if ( scale < 0 ) {
throw new IllegalArgumentException ( "The scale must be a positive integer or zero" ) ;
}
BigDecimal b = new BigDecimal ( Double. toString ( value) ) ;
BigDecimal one = new BigDecimal ( "1" ) ;
return b. divide ( one, scale, BigDecimal. ROUND_HALF_UP) . doubleValue ( ) ;
}
}