Tag: integer

Java:使用DecimalFormat格式化双精度和整数但保持整数不带小数分隔符

我正在尝试在Java程序中格式化一些数字。 数字将是双精度和整数。 处理双精度时,我想只保留两个小数点,但在处理整数时,我希望程序保持不受影响。 换一种说法: 双打 – 输入 14.0184849945 双打 – 输出 14.01 整数 – 输入 13 整数 – 输出 13 (not 13.00) 有没有办法在同一个 DecimalFormat实例中实现它? 到目前为止,我的代码如下: DecimalFormat df = new DecimalFormat(“#,###,##0.00”); DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH); otherSymbols.setDecimalSeparator(‘.’); otherSymbols.setGroupingSeparator(‘,’); df.setDecimalFormatSymbols(otherSymbols);

使用Integer.parseInt转换32位二进制字符串失败

为什么这部分代码失败: Integer.parseInt(“11000000000000000000000000000000”,2); Exception in thread “main” java.lang.NumberFormatException: For input string: “11000000000000000000000000000000” 据我所知,Integer是32位值。 上面代码中的0和1的数量是32.如果有31个代码可以工作。 为什么?

如何!=和==运算符在Java中使用整数?

以下代码似乎让我感到困惑,因为它提供了两个不同的输出。代码在jdk 1.7上进行了测试。 public class NotEq { public static void main(String[] args) { ver1(); System.out.println(); ver2(); } public static void ver1() { Integer a = 128; Integer b = 128; if (a == b) { System.out.println(“Equal Object”); } if (a != b) { System.out.println(“Different objects”); } if (a.equals(b)) { System.out.println(“Meaningfully equal.”); } } public static void […]

为什么等于运算符的Integer值直到128数?

为什么Integer “=”运算符不适用于128和Integer值之后? 有人可以解释这种情况吗? 这是我的Java环境:java版“1.6.0_37” Java(TM)SE运行时环境(版本1.6.0_37-b06) Java HotSpot(TM)64位服务器VM(内置20.12-b01,混合模式) 示例代码: Integer a; Integer b; a = 129; b = 129; for (int i = 0; i < 200; i++) { a = i; b = i; if (a != b) { System.out.println("Value:"+ i + " – Different values"); } else { System.out.println("Value"+ i + " Same values"); […]