我想知道如何在 Java 中获取单个值的余数和商。
例子:
3/2 我应该得到 1.5 的值。
如果我使用/
运算符,我只会得到商。如果我使用%
操作员,我只会得到剩余部分。如何在同一个变量中同时获得两者?
quotient = 3 / 2;
remainder = 3 % 2;
// now you have them both
在您的示例中,Java 正在执行整数运算,四舍五入除法的结果。
根据您的问题,您想执行浮点运算。为此,您的术语中的至少一项必须指定为(或转换为)浮点数:
指定浮点数:
3.0/2
3.0/2.0
3/2.0
转换为浮点数:
int a = 2;
int b = 3;
float q = ((float)a)/b;
或者
double q = ((double)a)/b;
(有关 和 的讨论,请参阅Java陷阱:double和Java Floating-Point Number Intricacies)float
double
您只需将int或long变量包装在BigDecimal对象中,然后对其调用divideAndRemainder方法。返回的数组将包含商和余数(按该顺序)。
Don't worry about it. In your code, just do the separate / and % operations as you mention, even though it might seem like it's inefficient. Let the JIT compiler worry about combining these operations to get both quotient and remainder in a single machine instruction (as far as I recall, it generally does).
如果你将两个参数都初始化为float
,你肯定会得到实际的分值。例如:
float RoomWidth, TileWidth, NumTiles;
RoomWidth = 142;
TileWidth = 8;
NumTiles = RoomWidth/TileWidth;
答:17.75。
你可以这样做,
int a = 3;
int b = 2;
int quotient = a / b;
int remainder = a % b;
得到实数的商
System.out.println((double) a / b);
获得整数的商
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
得到实数的商,使得小数点后有一个数字
System.out.println(a / b + "." + a % b * 10 / b);
注意:在最后一种方法中,它不会将小数点后的数字四舍五入。
我的意思是这很简单。将其设置为双精度。所以让我们说
double answer = 3.0/2.0;
System.out.print(answer);
int a = 3;
int b = 2;
float c = ((float)a)/b
请将您的输入转换为双倍并除以。
@recursive 的解决方案(接受的答案)是 100% 正确的。我只是添加一个示例代码供您参考。
我的情况是用两位小数显示价格。这是后端响应的一部分:"price": 2300, "currencySymbol": "CD", ...
。
这是我的助手类:
public class CurrencyUtils
{
private static final String[] suffix = { "", "K", "M" };
public static String getCompactStringForDisplay(final int amount)
{
int suffixIndex;
if (amount >= 1_000_000) {
suffixIndex = 2;
} else if (amount >= 1_000) {
suffixIndex = 1;
} else {
suffixIndex = 0;
}
int quotient;
int remainder;
if (amount >= 1_000_000) {
quotient = amount / 1_000_000;
remainder = amount % 1_000_000;
} else if (amount >= 1_000) {
quotient = amount / 1_000;
remainder = amount % 1_000;
} else {
return String.valueOf(amount);
}
if (remainder == 0) {
return String.valueOf(quotient) + suffix[suffixIndex];
}
// Keep two most significant digits
if (remainder >= 10_000) {
remainder /= 10_000;
} else if (remainder >= 1_000) {
remainder /= 1_000;
} else if (remainder >= 100) {
remainder /= 10;
}
return String.valueOf(quotient) + '.' + String.valueOf(remainder) + suffix[suffixIndex];
}
}
这是我的测试类(基于 Junit 4):
public class CurrencyUtilsTest {
@Test
public void getCompactStringForDisplay() throws Exception {
int[] numbers = {0, 5, 999, 1_000, 5_821, 10_500, 101_800, 2_000_000, 7_800_000, 92_150_000, 123_200_000, 9_999_999};
String[] expected = {"0", "5", "999", "1K", "5.82K", "10.50K", "101.80K", "2M", "7.80M", "92.15M", "123.20M", "9.99M"};
for (int i = 0; i < numbers.length; i++) {
int n = numbers[i];
String formatted = CurrencyUtils.getCompactStringForDisplay(n);
System.out.println(n + " => " + formatted);
assertEquals(expected[i], formatted);
}
}
}