我有這樣的代碼:的NumberFormat不給正確的數字
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.Scanner;
class EntryPoint {
static Locale czech = null;
static Locale getCzechLocale(){
if (czech == null){
czech = new Locale("cs","CZ");
}
return czech;
}
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
NumberFormat nf = NumberFormat.getInstance(getCzechLocale());
System.out.println(nf.format(12000.5));
NumberFormat nf2 = NumberFormat.getCurrencyInstance(getCzechLocale());
try {
String input = sn.nextLine();
System.out.println(input);
Number n = nf.parse(input);
System.out.println(n.floatValue());
}catch(ParseException javaTextParseException){
System.err.println("Unable to parse input String");
}
}
}
我要存檔這件事:如果我輸入數字「12 000,5」(這是節目顯示)程序解析和此String和意志顯示號碼「12000.5」。 我有什麼不對? 現在,如果我輸入「12 000,5」無「的標誌節目顯示浮點數‘12.0’而不是‘12000.5’
解決 NumberFormat.Parse(字符串)將結束於第一空間字符,所以如果我修剪()和替換(」」,‘’),那麼結果是正常
爲什麼你認爲這是錯的? – Henry
我在系統上使用「en_US」語言環境,所以我需要以「cs_CZ」格式輸入文本並在「en_US」中顯示 –
@亨利,因爲如果我輸入「12 000,5」,我得到結果「12.0」 –