Files
spring-boot-rest/spring-boot/src/main/java/org/baeldung/converter/GenericBigDecimalConverter.java
Michael C Good 73ae3cfb1a BAEL-1238 (#2941)
* michael.good703@gmail.com

michael.good703@gmail.com

* michael.good703@gmail.com

michael.good703@gmail.com

* michael.good703@gmail.com

michael.good703@gmail.com

* update

* michael.good703@gmail.com

Had to add @SpringBootApplication(exclude = MySQLAutoconfiguration.class)

* Updated for 3.3.0.Final

BAEL-1238

* Update pom.xml

* BAEL-1238

Added new module spring-boot-keycloak and removed Keycloak code from spring-boot module

* Minor changes to pom.xml

* Update CustomConverterTest.java

* Update StringToEmployeeConverter.java

* Update GenericBigDecimalConverter.java

* Update MyFeatures.java

* Update .gitignore

* Formatting changes

* "Resolving conflicts"

* Updated spring-boot to remove keycloak

* Updated to see

* Update

* Updated

* Found remnant file and deleted it
2017-11-03 07:09:31 -07:00

38 lines
1.2 KiB
Java

package org.baeldung.converter;
import com.google.common.collect.ImmutableSet;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Set;
public class GenericBigDecimalConverter implements GenericConverter {
@Override
public Set<ConvertiblePair> getConvertibleTypes () {
ConvertiblePair[] pairs = new ConvertiblePair[] {
new ConvertiblePair(Number.class, BigDecimal.class),
new ConvertiblePair(String.class, BigDecimal.class)};
return ImmutableSet.copyOf(pairs);
}
@Override
public Object convert (Object source, TypeDescriptor sourceType,
TypeDescriptor targetType) {
if (sourceType.getType() == BigDecimal.class) {
return source;
}
if(sourceType.getType() == String.class) {
String number = (String) source;
return new BigDecimal(number);
} else {
Number number = (Number) source;
BigDecimal converted = new BigDecimal(number.doubleValue());
return converted.setScale(2, BigDecimal.ROUND_HALF_EVEN);
}
}
}