gpt4 book ai didi

java - 如何比较表示为 long 和 int 的小数?

转载 作者:行者123 更新时间:2023-11-30 08:01:07 25 4
gpt4 key购买 nike

有没有办法可以比较(>、<、>=、<=、!=、==)表示为 long 和 int 的小数?

如果数字是 3214.21 那么它将在这样的类中表示

long units = 321421;
int precision = 2;
// to get the original number I would do units * 10^precision

我希望能够执行类似于 BigDecimal 的compareTo() 方法的操作。因此大于返回 1,等于返回 0,小于返回 -1。

我目前所做的事情在某些情况下不起作用。下面概述了使其以这种方式运行的代码。该方法或多或少是一个概念证明。

public int compareTo(Money other) {
if (precision == other.getPrecision()) { // fast check if precision is the same
if (units > other.getUnits()) return 1; // we forgot to inverse/flip here. will be an issue for non-decimal
else if (units < other.getUnits()) return -1;
else return 0; // least likely
}

int intX = (int) (units / (Math.pow(10, precision))); // converted units whole numbers to int
int fractionX = (int) (units % (Math.pow(10, precision))); // converts the decimal as an int

int intY = (int) (other.getUnits() / (Math.pow(10, other.getPrecision()))); // converted units whole numbers to int
int fractionY = (int) (other.getUnits() % (Math.pow(10, other.getPrecision()))); // converts the decimal as an int

System.out.println("Test: i " + intX + "| f " + fractionX + "| u " + units + "| p " + precision);
System.out.println("Test2: i " + intY + "| f " + fractionY + "| u " + other.getUnits() + "| p" + other
.getPrecision
());

if (intX > intY) return 1;
else if (intX < intY) return -1;
else {
if (fractionX > fractionY) return 1; // this is where the logic fails
if (fractionX < fractionY) return -1;
else return 0;
}
}

这是我的测试以及输出

System.out.println(MoneyFactory.fromString("0.3").compareTo(MoneyFactory.fromString("0.29")));

System.out.println(MoneyFactory.fromString("13").compareTo(MoneyFactory.fromString("0.31456789")));

System.out.println(MoneyFactory.fromString("0.2999").compareTo(MoneyFactory.fromString
("0.3")));

输出

Test: i 0| f 3| u 3| p 1
Test2: i 0| f 29| u 29| p2
-1
Test: i 13| f 0| u 13| p 0
Test2: i 0| f 31456789| u 31456789| p8
1
Test: i 0| f 2999| u 2999| p 4
Test2: i 0| f 3| u 3| p1
1

最佳答案

最简单的解决方案是将一个数字转换为通用精度级别,然后比较这些数字。如果它们相同,则使用“具有更大精度的数字”逻辑(伪代码):

return (number1 == number2) ? [number with bigger precision logic] : number1 - number2

在Java代码中

class Money {
long units;
int precision;

public Money (long un, int prec) {
units = un;
precision = prec;
}

public int compareTo(Money other) {
int preResult = this.precision - other.precision;
long first = (preResult > 0) ? ((long)(this.units / Math.pow(10, preResult))) : this.units;
long second = (preResult < 0) ? ((long)(other.units * Math.pow(10, preResult))) : other.units;
return (first == second) ? preResult : Long.compare(first, second);
}

public static void test() {
Money first = new Money(2345L, 4);
Money second = new Money(234567L, 6);
System.out.println(first.compareTo(second));
}

}

编辑:代码中有错误。将两个长期检查中的 1 更改为 0 可修复此问题

关于java - 如何比较表示为 long 和 int 的小数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31910336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com