Wednesday, December 9, 2009

Currency format

As we know the way of formatting currency are different in many country. Main there is a big difference in European and British way. This code snippet converts the currency to appropriate formats and also does the reverse parsing.



 
public static void main(String args[]){
  // Format
     Locale locale = Locale.FRANCE;
     String string1 = NumberFormat.getCurrencyInstance(locale).format(123456789.12);
     System.out.println(string1);
    
     locale = Locale.US;
     String string = NumberFormat.getCurrencyInstance(locale).format(123456789.12);
     System.out.println(string);
    
     // Parse
     try {
         Number number = NumberFormat.getCurrencyInstance(locale).parse(string1);
         System.out.println(number.toString());
         number = NumberFormat.getCurrencyInstance(locale).parse(string);
         System.out.println(number.toString());
     } catch (ParseException e) {
     }
 }
 
 

No comments: