Java本地化数字格式 [英] Java localizing number formatting

查看:146
本文介绍了Java本地化数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java使用小数点的句点,例如1/2 = 0.5

Java uses period in decimals, e.g. 1/2 = 0.5

有没有办法使它使用逗号代替,例如1/2 = 0,5?而不是使用千位逗号(十万= 100,000),而要使用空格(100 000)?

Is there any way to make it use comma instead, as in 1/2 = 0,5? And not to use comma for thousands (as in one hundred thousand = 100,000) but use space instead (100 000)?

关于输出,我想我可以使用各种字符串格式函数,但是问题出在输入(JTable)上.有些列要求使用Double格式,因此用户必须输入45.5之类的内容,在这些部分中它们使用的是45.5 :)预先感谢

When it comes to output I suppose I could use all sorts of string format functions, but the problem is input (JTable). Some columns require Double format so users must enter something like 45.5 and in these parts they are used to 45,5 :) Thanks in advance

更新:

我尝试使用myTable.setDefaultLocale(Locale.Germany);但它没有用.我也做了Locale.setDefault(Locale.Germany); @ main函数,它确实起作用,但是很愚蠢:在单元格处于编辑模式时,必须正常输入点,即45.5,但是在按之后按下Enter确认更改,它显示为逗号:45,5.我的意思是,它仅将逗号用于显示目的,但是在编辑其相同点时会使用逗号.

I tried using myTable.setDefaultLocale(Locale.Germany); but it didnt work. I also did Locale.setDefault(Locale.Germany); @ main function and it did work but in rather silly way: while cell is in editing mode, you must enter dot as normal, i.e. 45.5 but after you hit enter to confirm changes, it is displayed as comma: 45,5. I mean it uses comma only for display purposes, but when editing its still same ol' dot.

有什么方法可以解决 而无需编写自定义表格模型吗?

Is there any way to fix it without writing custom table model?

推荐答案

因此,您基本上想将本地化 String表示形式转换数字格式转换为Number/BigDecimal,反之亦然?

Thus, you basically want to convert a localized String representation supposedly in a numerical format into a Number/BigDecimal and vice versa?

您有 java.text.DecimalFormat .要了解更多信息,请参考 Sun自己的教程有关主题.

There you have the java.text.DecimalFormat for. To learn more, consult Sun's own tutorial about the subject.

要本地化Swing应用程序,请使用

To localize your Swing application, use JComponent#setDefaultLocale(). E.g.

JComponent.setDefaultLocale(Locale.GERMANY);

这篇关于Java本地化数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆