Monday, December 04, 2006

How to force decimal separator char upon java region code?

Java application could be run on any compliant platform. And that's great. But, when you provide it to your customer, you don't know the region code of his server. For exemple, you don't know if is configured to work in english or french.

By the way, let's take the previous example. In franch, decimal separator char is ',' (comma). In english, it is '.' (dot). When prensenting data to end user, you probably want to ensure that the decimal separator char is the right. So, you can for it usiing DecimalFormat.

Let's see it in the following code :

public class AfficheurDecimal {

public AfficheurDecimal() {
testerReste(100);
testerReste(0);
testerReste(12354.12854);
}

private void testerReste(double reste) {
if (reste != 0) {
DecimalFormat formatEntier = new DecimalFormat("0.00");
DecimalFormatSymbols symbols = formatEntier.getDecimalFormatSymbols();
symbols.setDecimalSeparator(',');
formatEntier.setDecimalFormatSymbols(symbols);
String msg = "Il reste " + formatEntier.format(reste) + " à ventiler";
System.out.println(msg);
}
if (reste == 0) {
System.out.println("Pas de reste");
}
}


/**
* Auteur : a.depellegrin

* Définition :

* @param args
*/
public static void main(String[] args) {
new AfficheurDecimal();
}

}

That's all!

Display Javascript console logs with jsfiddle

Jsfiddle is mainly made to test javascript code in a web context (aka with HTML and CSS). But, if you just want to use is to test backend c...