删除多余方法,修改方法名。

dev
ZhouXY108 2024-10-18 18:40:41 +08:00
parent cfc1ddc6b3
commit f2298e934c
2 changed files with 79 additions and 222 deletions

View File

@ -17,9 +17,6 @@
package xyz.zhouxy.plusone.commons.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
@ -28,7 +25,7 @@ public class BigDecimals {
public static final BigDecimal ZERO = new BigDecimal("0.00");
public static boolean equals(@Nullable BigDecimal a, @Nullable BigDecimal b) {
public static boolean equalsValue(@Nullable BigDecimal a, @Nullable BigDecimal b) {
return (a == b) || (a != null && a.compareTo(b) == 0);
}
@ -39,7 +36,7 @@ public class BigDecimals {
}
public static boolean ge(BigDecimal a, BigDecimal b) {
return gt(a, b) || equals(a, b);
return gt(a, b) || equalsValue(a, b);
}
public static boolean lt(BigDecimal a, BigDecimal b) {
@ -49,224 +46,13 @@ public class BigDecimals {
}
public static boolean le(BigDecimal a, BigDecimal b) {
return lt(a, b) || equals(a, b);
return lt(a, b) || equalsValue(a, b);
}
public static BigDecimal of(final String val) {
return (StringTools.isNotBlank(val)) ? new BigDecimal(val) : ZERO;
}
/**
*
*/
private static final int DEFAULT_STR_SCALE = 2;
private static final RoundingMode DEFAULT_STR_ROUNDING_MODE = RoundingMode.HALF_UP;
/**
* 使{@link RoundingMode#HALF_UP}
*/
public static String toPlainString(@Nonnull BigDecimal value) {
AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null.");
return toPlainStringInternal(value, DEFAULT_STR_SCALE, DEFAULT_STR_ROUNDING_MODE);
}
/**
* 使{@link RoundingMode#HALF_UP}
*/
public static String toPlainString(@Nonnull BigDecimal value, int scale) {
AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null.");
return toPlainStringInternal(value, scale, DEFAULT_STR_ROUNDING_MODE);
}
/**
* 使 {@link RoundingMode}
*/
public static String toPlainString(@Nonnull BigDecimal value, @Nonnull RoundingMode roundingMode) {
AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null.");
AssertTools.checkArgumentNotNull(roundingMode, "The argument \"rounding mode\" cannot be null.");
return toPlainStringInternal(value, DEFAULT_STR_SCALE, roundingMode);
}
/**
* 使 {@link RoundingMode}
*/
public static String toPlainString(@Nonnull BigDecimal value,
int scale, @Nonnull RoundingMode roundingMode) {
AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null.");
AssertTools.checkArgumentNotNull(roundingMode, "The argument \"rounding mode\" cannot be null.");
return toPlainStringInternal(value, scale, roundingMode);
}
private static String toPlainStringInternal(@Nonnull BigDecimal value,
int scale, @Nonnull RoundingMode roundingMode) {
return value.setScale(scale, roundingMode).toPlainString();
}
/**
*
*/
private static final int DEF_DIV_SCALE = 10;
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double add(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.add(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double subtract(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.subtract(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double multiply(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.multiply(b2).doubleValue();
}
/**
*
* 10
*
* @param v1
* @param v2
* @return
*/
public static double divide(double v1, double v2) {
return divide(v1, v2, DEF_DIV_SCALE);
}
/**
* scale
*
*
* @param v1
* @param v2
* @param scale
* @return
*/
public static double divide(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.divide(b2, scale, RoundingMode.HALF_EVEN).doubleValue();
}
/**
*
*
* @param v
* @param scale
* @return
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = BigDecimal.valueOf(v);
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
}
/**
* (Float)
*
* @param v
* @return
*/
public static float convertToFloat(double v) {
BigDecimal b = BigDecimal.valueOf(v);
return b.floatValue();
}
/**
* (Int)
*
* @param v
* @return
*/
public static int convertsToInt(double v) {
BigDecimal b = BigDecimal.valueOf(v);
return b.intValue();
}
/**
* (Long)
*
* @param v
* @return
*/
public static long convertsToLong(double v) {
BigDecimal b = BigDecimal.valueOf(v);
return b.longValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double returnMax(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.max(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return
*/
public static double returnMin(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.min(b2).doubleValue();
}
/**
*
*
* @param v1
* @param v2
* @return 01-1
*/
public static int compareTo(double v1, double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.compareTo(b2);
}
private BigDecimals() {
throw new IllegalStateException("Utility class");
}

View File

@ -18,16 +18,87 @@ package xyz.zhouxy.plusone.commons.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class BigDecimalsTests {
@Test
void testToPlainString() {
assertEquals("8.09", BigDecimals.toPlainString(BigDecimals.of("8.090")));
assertEquals("8.09", BigDecimals.toPlainString(BigDecimals.of("8.094")));
assertEquals("8.10", BigDecimals.toPlainString(BigDecimals.of("8.095")));
assertEquals("8.10", BigDecimals.toPlainString(BigDecimals.of("8.096")));
assertEquals("8.10", BigDecimals.toPlainString(BigDecimals.of("8.100")));
BigDecimalFormatter formatter = BigDecimalFormatter.builder()
.setScale(2, RoundingMode.HALF_UP)
.stripTrailingZeros()
.build();
assertEquals("8.09", formatter.toPlainString(BigDecimals.of("8.090")));
assertEquals("8.09", formatter.toPlainString(BigDecimals.of("8.094")));
assertEquals("8.1", formatter.toPlainString(BigDecimals.of("8.095")));
assertEquals("8.1", formatter.toPlainString(BigDecimals.of("8.096")));
assertEquals("8.1", formatter.toPlainString(BigDecimals.of("8.100")));
}
@Test
void test() {
Object a = 100 % 3.0;
log.info("a: {}", a);
}
}
class BigDecimalFormatter {
private final Function<BigDecimal, BigDecimal> func;
private BigDecimalFormatter(Function<BigDecimal, BigDecimal> wholeFunc) {
this.func = wholeFunc;
}
public static Builder builder() {
return new Builder();
}
public String toPlainString(BigDecimal value) {
final BigDecimal finalDecimal = func == null ? value : func.apply(value);
return finalDecimal.toPlainString();
}
public String toEngineeringString(BigDecimal value) {
final BigDecimal finalDecimal = func == null ? value : func.apply(value);
return finalDecimal.toEngineeringString();
}
public static class Builder {
private Function<BigDecimal, BigDecimal> wholeFunc;
private Builder() {
}
public Builder setScale(int newScale, RoundingMode roundingMode) {
final Function<BigDecimal, BigDecimal> func = value -> value.setScale(newScale, roundingMode);
if (wholeFunc == null) {
wholeFunc = func;
} else {
wholeFunc = func.andThen(func);
}
return this;
}
public Builder stripTrailingZeros() {
if (wholeFunc == null) {
wholeFunc = BigDecimal::stripTrailingZeros;
} else {
wholeFunc = wholeFunc.andThen(BigDecimal::stripTrailingZeros);
}
return this;
}
public BigDecimalFormatter build() {
return new BigDecimalFormatter(wholeFunc);
}
}
}