English 中文(简体)
Java localizing number formatting
原标题:

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

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)?

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

Update:

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?

最佳回答

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

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

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

JComponent.setDefaultLocale(Locale.GERMANY);
问题回答

Take a look at 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) {
}

To process the input correctly, you can implement your own TableModel and overwrite the setValueAt method.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签