[JDK6] Formattage en monétaire
Salut,
Je veux formatter mes chaines qui correspondent à des montants en euros, provenant du base de données.
exemple :
Citation:
52.4 -> 52,40
1524.6 -> 1 524,60
541215.657 -> 541 215,66
Pour réaliser ça, j'utilise ce code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public static String formatMoneyValue(String value)
{
String formatValue=null;
DecimalFormatSymbols FR_FMT_SYMBOLS = new DecimalFormatSymbols(Locale.FRANCE);
DecimalFormat MONEY_FORMATTER = new DecimalFormat("#,##0.00",FR_FMT_SYMBOLS);
try
{
value=value.replace(',', '.');
formatValue=MONEY_FORMATTER.format(Double.valueOf(value));
}
catch(NumberFormatException exc)
{
formatValue=null;
}
catch(IllegalArgumentException exc)
{
formatValue=null;
}
return formatValue;
} |
Tout marche bien mais si vous avez un code mieux :mrgreen: je suis preneur ..
Et voici mon problème en test unitaire:
Code:
1 2 3 4 5 6 7
|
public void testFormatMoneyValue3()
{
String value="5452.256";
System.out.println("v="+StringUtil.formatMoneyValue(value));
Assert.assertEquals("5 452,26", StringUtil.formatMoneyValue(value));
} |
Mon test est toujours FAUX alors que l'affichage de la variable v=5 452,26.
:calim2: je ne comprends pas pourquoi ???
tout aide, conseils sont les bienvenus...