我用prod代码玩Java 9.我发现一些格式化测试失败了.经过一些研究,我能够创建一个类来重现这个问题.这种情况发生在Java 9中,而不是Java 8中.
该课程在这里 https://pastebin.com/87sA5WMb
import java.text.*;
import java.util.*;
public class FormatFails {
public static void main(String... args){
Currency currency = Currency.getInstance("EUR");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.FRANCE);
currencyFormatter.setMaximumFractionDigits(0);
currencyFormatter.setMinimumFractionDigits(0);
currencyFormatter.setCurrency(currency);
String expected = "123 457 €";
String obtained = currencyFormatter.format(123456.789);
System.out.println(expected);
System.out.println(obtained);
System.out.println(expected.equals(obtained));
System.out.format("Bytes from expected: %s\n",Arrays.toString(expected.getBytes()));
System.out.format("Bytes from obtained: %s\n",Arrays.toString(obtained.getBytes()));
}
}
Run Code Online (Sandbox Code Playgroud)
基本上比较2个字符串:
因为以下原因,在两者上使用equals失败:
123 457 €
123 457 €
false
Bytes from expected: [49, 50, 51, -62, -96, 52, 53, 55, 32, -30, -126, -84]
Bytes …Run Code Online (Sandbox Code Playgroud)