Java 使用小数形式的句点,例如1/2 = 0.5
有没有办法让它使用逗号代替,如 1/2 = 0,5?而不是使用逗号表示千(如十万 = 100,000)而是使用空格代替(100 000)?
关于输出,我想我可以使用各种字符串格式函数,但问题是输入 (JTable)。有些列需要 double 格式,因此用户必须输入类似 45.5 的内容,在这些部分中,他们习惯使用 45,5 :) 提前致谢
更新:
我尝试使用 myTable.setDefaultLocale(Locale.Germany);但它没有用。我也做了 Locale.setDefault(Locale.Germany); @main 函数,它确实起作用了,但方式相当愚蠢:当单元格处于编辑模式时,您必须像往常一样输入点,即 45.5,但是在您按下回车键确认更改后,它显示为逗号:45,5。我的意思是它仅出于显示目的使用逗号,但在编辑时仍然使用相同的 ol' 点。
有什么方法可以不编写自定义表模型来修复它吗?
最佳答案
看看Formatting and Parsing a Number for a Locale :
// Format for CANADA locale Locale locale = Locale.CANADA; String string = NumberFormat.getNumberInstance(locale).format(-1234.56); // -1,234.56 // Format for GERMAN locale locale = Locale.GERMAN; string = NumberFormat.getNumberInstance(locale).format(-1234.56); // -1.234,56 // Format for the default locale string = NumberFormat.getNumberInstance().format(-1234.56); // Parse a GERMAN number try { Number number = NumberFormat.getNumberInstance(locale.GERMAN).parse("-1.234,56"); if (number instanceof Long) { // Long value } else { // Double value } } catch (ParseException e) { }
关于Java本地化数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2076032/