DecimalTools

Die Klasse DecimalTools stellt statische Hilfsmethoden Methoden zur Verfügung, um Datentypen String, Integer, Long, Float und Double in BigDecimal oder in einen String zu parsen.

DecimalTools.java


package de.snowbits.tools;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.function.Function;

public class DecimalTools {

	private static final Function<String, BigDecimal> fnDecimal = (value) -> new BigDecimal(value);
	
	private DecimalTools() {
	}
	
	public static final BigDecimal decimal(String value, int scale) {
		return decimal(value, scale, RoundingMode.HALF_UP);
	}
	
	public static final BigDecimal decimal(String value, int scale, RoundingMode roundingMode) {
		return parse(value, scale, roundingMode);
	}
	
	public static final BigDecimal decimal(Integer value, int scale) {
		return decimal(value, scale, RoundingMode.HALF_UP);
	}
	
	public static final BigDecimal decimal(Integer value, int scale, RoundingMode roundingMode) {
		return parse(value, scale, roundingMode);
	}
	
	public static final BigDecimal decimal(Long value, int scale) {
		return decimal(value, scale, RoundingMode.HALF_UP);
	}
	
	public static final BigDecimal decimal(Long value, int scale, RoundingMode roundingMode) {
		return parse(value, scale, roundingMode);
	}

	public static final BigDecimal decimal(Float value, int scale) {
		return decimal(value, scale, RoundingMode.HALF_UP);
	}
	
	public static final BigDecimal decimal(Float value, int scale, RoundingMode roundingMode) {
		return parse(value, scale, roundingMode);
	}
	
	public static final BigDecimal decimal(Double value, int scale) {
		return decimal(value, scale, RoundingMode.HALF_UP);
	}
	
	public static final BigDecimal decimal(Double value, int scale, RoundingMode roundingMode) {
		return parse(value, scale, roundingMode);
	}

	private static final <T> BigDecimal parse(T value, int scale, RoundingMode roundingMode) {
		if (scale < 1)
			throw new IllegalArgumentException("Error, scale must greater than 0: scale = " + scale);

		return fnDecimal.apply(String.valueOf(value).replaceAll(",", ".")).setScale(scale, roundingMode);
	}

}
	

DecimalToolsTest.java


package de.snowbits.tools;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

public class DecimalToolsTest {
	
	private static final int SCALE_ONE   = 1;
	private static final int SCALE_TWO   = 2;
	private static final int SCALE_THREE = 3;

	private static final int SCALE_ZERO     = 0;
	private static final int SCALE_NEGATIVE = -1;
	
	private static final String TESTEE_STRING   = "99.123456789";
	private static final Integer TESTEE_INTEGER = 991234567;
	private static final Long TESTEE_LONG       = 99123456789L;
	private static final Float TESTEE_FLOAT     = 99.123456789F;
	private static final Double TESTEE_DOUBLE   = 99.123456789D;
	
	private String _result;
	@Before
	public void setUp() throws Exception {
		_result = null;
	}

	@Test
	public void decimalString() {
		_result = DecimalTools.decimal(TESTEE_STRING, SCALE_ONE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_ONE));
		_result = DecimalTools.decimal(TESTEE_STRING, SCALE_TWO).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_TWO));
		_result = DecimalTools.decimal(TESTEE_STRING, SCALE_THREE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_THREE));
	}
	
	@Test
	public void decimalInteger() {
		_result = DecimalTools.decimal(TESTEE_INTEGER, SCALE_ONE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_ONE));
		_result = DecimalTools.decimal(TESTEE_INTEGER, SCALE_TWO).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_TWO));
		_result = DecimalTools.decimal(TESTEE_INTEGER, SCALE_THREE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_THREE));
	}

	@Test
	public void decimalLong() {
		_result = DecimalTools.decimal(TESTEE_LONG, SCALE_ONE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_ONE));
		_result = DecimalTools.decimal(TESTEE_LONG, SCALE_TWO).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_TWO));
		_result = DecimalTools.decimal(TESTEE_LONG, SCALE_THREE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_THREE));
	}

	@Test
	public void decimalFloat() {
		_result = DecimalTools.decimal(TESTEE_FLOAT, SCALE_ONE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_ONE));
		_result = DecimalTools.decimal(TESTEE_FLOAT, SCALE_TWO).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_TWO));
		_result = DecimalTools.decimal(TESTEE_FLOAT, SCALE_THREE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_THREE));
	}

	@Test
	public void decimalDouble() {
		_result = DecimalTools.decimal(TESTEE_DOUBLE, SCALE_ONE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_ONE));
		_result = DecimalTools.decimal(TESTEE_DOUBLE, SCALE_TWO).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_TWO));
		_result = DecimalTools.decimal(TESTEE_DOUBLE, SCALE_THREE).toPlainString();
		Assert.assertTrue(isDecimal(SCALE_THREE));
	}

	@Test(expected = IllegalArgumentException.class)
	public void decimalLessThan1() {
		DecimalTools.decimal(TESTEE_STRING, SCALE_ZERO).toPlainString();
		DecimalTools.decimal(TESTEE_STRING, SCALE_NEGATIVE).toPlainString();
		DecimalTools.decimal(TESTEE_INTEGER, SCALE_ZERO).toPlainString();
		DecimalTools.decimal(TESTEE_INTEGER, SCALE_NEGATIVE).toPlainString();
		DecimalTools.decimal(TESTEE_LONG, SCALE_ZERO).toPlainString();
		DecimalTools.decimal(TESTEE_LONG, SCALE_NEGATIVE).toPlainString();
		DecimalTools.decimal(TESTEE_FLOAT, SCALE_ZERO).toPlainString();
		DecimalTools.decimal(TESTEE_FLOAT, SCALE_NEGATIVE).toPlainString();
		DecimalTools.decimal(TESTEE_DOUBLE, SCALE_ZERO).toPlainString();
		DecimalTools.decimal(TESTEE_DOUBLE, SCALE_NEGATIVE).toPlainString();
	}
	
	private boolean isDecimal(int scaled) {
		if (_result.indexOf(".") == -1)
			return false;
		
		 String[] array = _result.replace(".", ",").split(",");
		
		if (array.length != 2)
			return false;
		
		if (array[1].length() != scaled)
			return false;
		
		return true;
	}
}
	

Top


Example
Sitemap Kontakt Impressum