Skip to content

Commit e7a35c9

Browse files
awalter17ctrueden
authored andcommitted
Implemented asBigInteger and asBigDecimal in converters
1 parent 93318d2 commit e7a35c9

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/main/java/org/scijava/convert/NumberToBigDecimalConverter.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
package org.scijava.convert;
3333

3434
import java.math.BigDecimal;
35-
import java.math.BigInteger;
35+
36+
import org.scijava.util.NumberUtils;
3637

3738
/**
3839
* Converts numbers to BigDecimals.
@@ -43,15 +44,7 @@ public abstract class NumberToBigDecimalConverter<N extends Number> extends Numb
4344

4445
@Override
4546
public BigDecimal convert(Number n) {
46-
// cannot get doubleValue of a BigInteger
47-
if(BigInteger.class.isInstance(n)){
48-
return new BigDecimal((BigInteger) n);
49-
}
50-
// Using .doubleValue on a long would cause loss of accuracy
51-
else if(Long.class.isInstance(n)){
52-
return new BigDecimal(n.longValue());
53-
}
54-
return new BigDecimal(n.doubleValue());
47+
return NumberUtils.asBigDecimal(n);
5548
}
5649

5750
@Override

src/main/java/org/scijava/convert/NumberToBigIntegerConverter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
* %%
99
* Redistribution and use in source and binary forms, with or without
1010
* modification, are permitted provided that the following conditions are met:
11-
*
11+
*
1212
* 1. Redistributions of source code must retain the above copyright notice,
1313
* this list of conditions and the following disclaimer.
1414
* 2. Redistributions in binary form must reproduce the above copyright notice,
1515
* this list of conditions and the following disclaimer in the documentation
1616
* and/or other materials provided with the distribution.
17-
*
17+
*
1818
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -33,21 +33,25 @@
3333

3434
import java.math.BigInteger;
3535

36+
import org.scijava.util.NumberUtils;
37+
3638
/**
3739
* Converts numbers to BigIntegers.
3840
*
3941
* @author Alison Walter
4042
*/
41-
public abstract class NumberToBigIntegerConverter<N extends Number> extends NumberToNumberConverter<N, BigInteger> {
43+
public abstract class NumberToBigIntegerConverter<N extends Number> extends
44+
NumberToNumberConverter<N, BigInteger>
45+
{
4246

4347
@Override
44-
public BigInteger convert(Number n) {
45-
return BigInteger.valueOf(n.longValue());
48+
public BigInteger convert(final Number n) {
49+
return NumberUtils.asBigInteger(n);
4650
}
4751

4852
@Override
4953
public Class<BigInteger> getOutputType() {
5054
return BigInteger.class;
5155
}
5256

53-
}
57+
}

0 commit comments

Comments
 (0)