Skip to content

Commit

Permalink
Grouped Number Conversion tests, changed BigItneger to trunc to mastc…
Browse files Browse the repository at this point in the history
…h integer types
  • Loading branch information
kpartlow committed Jan 26, 2024
1 parent 5d2e406 commit dda4781
Show file tree
Hide file tree
Showing 5 changed files with 538 additions and 449 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static AtomicLong toAtomicLong(Object from, Converter converter, ConverterOption

static Character toCharacter(Object from, Converter converter, ConverterOptions options) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.CHARACTER_ONE : CommonValues.CHARACTER_ZERO;
return b.get() ? options.trueChar() : options.falseChar();
}

static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, BigDecimal.class), ZonedDateTimeConversions::toBigDecimal);
DEFAULT_FACTORY.put(pair(UUID.class, BigDecimal.class), UUIDConversions::toBigDecimal);
DEFAULT_FACTORY.put(pair(Calendar.class, BigDecimal.class), CalendarConversions::toBigDecimal);
DEFAULT_FACTORY.put(pair(Number.class, BigDecimal.class), NumberConversions::bigDecimalToBigDecimal);
DEFAULT_FACTORY.put(pair(Number.class, BigDecimal.class), NumberConversions::toBigDecimal);
DEFAULT_FACTORY.put(pair(Map.class, BigDecimal.class), MapConversions::toBigDecimal);
DEFAULT_FACTORY.put(pair(String.class, BigDecimal.class), StringConversions::toBigDecimal);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cedarsoftware.util.convert;

import com.cedarsoftware.util.StringUtilities;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
Expand Down Expand Up @@ -121,8 +123,8 @@ static BigInteger bigDecimalToBigInteger(Object from, Converter converter, Conve
return ((BigDecimal)from).toBigInteger();
}

static BigDecimal bigDecimalToBigDecimal(Object from, Converter converter, ConverterOptions options) {
return new BigDecimal(from.toString());
static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOptions options) {
return new BigDecimal(StringUtilities.trimToEmpty(from.toString()));
}

static AtomicBoolean toAtomicBoolean(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -134,7 +136,9 @@ static BigDecimal floatingPointToBigDecimal(Object from, Converter converter, Co
}

static BigInteger floatingPointToBigInteger(Object from, Converter converter, ConverterOptions options) {
return new BigInteger(String.format("%.0f", ((Number)from).doubleValue()));
double d = toDouble(from);
String s = String.format("%.0f", (d > 0.0) ? Math.floor(d) : Math.ceil(d));
return new BigInteger(s);
}

static boolean isIntTypeNotZero(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -154,7 +158,7 @@ static boolean isBigDecimalNotZero(Object from, Converter converter, ConverterOp
}

static BigInteger toBigInteger(Object from, Converter converter, ConverterOptions options) {
return new BigInteger(from.toString());
return new BigInteger(StringUtilities.trimToEmpty(from.toString()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static Boolean toBoolean(Object from, Converter converter, ConverterOptions opti
static char toCharacter(Object from, Converter converter, ConverterOptions options) {
String str = StringUtilities.trimToEmpty((String)from);
if (str.isEmpty()) {
return (char) 0;
return CommonValues.CHARACTER_ZERO;
}
if (str.length() == 1) {
return str.charAt(0);
Expand Down
Loading

0 comments on commit dda4781

Please sign in to comment.