Ai đó có thể giải thích cho tôi tại sao mã dưới đây cung cấp kết quả này?Số phân tích cú pháp có hậu tố âm
1.2
null
Chạy đoạn mã sau:
String positive = "1.2+";
String negative = "1.2-";
DecimalFormat format = new DecimalFormat("0.0");
format.setPositiveSuffix("+");
format.setNegativeSuffix("-");
format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US));
System.out.println(format.parse(positive, new ParsePosition(0)));
System.out.println(format.parse(negative, new ParsePosition(0)));
này hoạt động mặc dù, nhưng tôi không thích sự lặp lại của các mô hình:
String positive = "1.2+";
String negative = "1.2-";
DecimalFormat format = new DecimalFormat("0.0+;0.0-");
format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US));
System.out.println(format.parse(positive, new ParsePosition(0)));
System.out.println(format.parse(negative, new ParsePosition(0)));
là hậu tố không dự định được sử dụng cho phân tích cú pháp?
phân tích cú pháp không được đảm bảo để tiêu thụ toàn bộ chuỗi. Nó sẽ dừng lại khi nó được một nhân vật nó không biết phải làm gì với. –
@PeterLawrey Tôi biết, nhưng tôi nghĩ tôi đã nói với nó phải làm gì khi tôi thiết lập hậu tố? Và tại sao nó xử lý + nhưng không -? –
@PeterLawrey Tôi cũng giả định rằng mã thứ hai là chậm hơn, vì nó sẽ cần phải phân tích cú pháp toàn bộ chuỗi hai lần? –