DecimalToBigDecimal extends TypeConverterBase { @Override public Object to(byte[] bytes, int offset, int length, int decimals, boolean removePadding) { if(this.defaultValue != null && ByteUtils.allEquals(bytes, this.nullFillerByte, offset, bytes.length)) { return new BigDecimal(defaultValue); } else { return new BigDecimal(new BigInteger(getString(bytes, offset, length, removePadding, 1)), decimals); } }  @Override void validate(Class<?> type, int size, int decimals); @Override Object to(byte[] bytes, int offset, int length, int decimals, boolean removePadding); @Override byte[] from(Object value, int length, int decimals, boolean addPadding);  }
@Test public void testTo() throws Exception { assertEquals(new BigDecimal("1.1"), typeConverter.to("11".getBytes(StandardCharsets.UTF_8), 0, 2, 1, true)); assertEquals(new BigDecimal("10.01"), typeConverter.to("1001".getBytes(StandardCharsets.UTF_8), 0, 4, 2, true)); assertEquals(new BigDecimal("10.00001"), typeConverter.to("1000001".getBytes(StandardCharsets.UTF_8), 0, 7, 5, true)); assertEquals(new BigDecimal("10.10000"), typeConverter.to("1010000".getBytes(StandardCharsets.UTF_8), 0, 7, 5, true)); assertEquals(new BigDecimal("101000.0"), typeConverter.to("1010000".getBytes(StandardCharsets.UTF_8), 0, 7, 1, true)); }
@Test public void testToSubOne() throws Exception { assertEquals(new BigDecimal("0.01"), typeConverter.to("0001".getBytes(StandardCharsets.UTF_8), 0, 4, 2, true)); assertEquals(new BigDecimal("0.10"), typeConverter.to("0010".getBytes(StandardCharsets.UTF_8), 0, 4, 2, true)); assertEquals(new BigDecimal("0.00001"), typeConverter.to("0000001".getBytes(StandardCharsets.UTF_8), 0, 7, 5, true)); assertEquals(new BigDecimal("0.01100"), typeConverter.to("0001100".getBytes(StandardCharsets.UTF_8), 0, 7, 5, true)); }
@Test public void testToZeroValue() throws Exception { assertEquals(new BigDecimal("0.00"), typeConverter.to("0000000000".getBytes(StandardCharsets.UTF_8), 0, 9, 2, true)); }
@Test public void testToNullDefaultValue() throws Exception { config.setNullFillerChar((char)0); config.setDefaultValue("42.00"); typeConverter.initialize(config); assertEquals(new BigDecimal("42.00"), typeConverter.to(new byte[4], 0, 2, 2, true)); }
@Test public void testToNullValue() throws Exception { expectedEx.expect(NumberFormatException.class); expectedEx.expectMessage("For input string"); config.setNullFillerChar((char)0); typeConverter.initialize(config); assertEquals(null, typeConverter.to(new byte[4], 0, 2, 2, true)); }