Search code examples
javaarraylistlocalebigdecimal

BigDecimal/ArrayList needs new Locale


I have an ArrayList in which I add BigDecimals.

My problem is, that the BigDecimals use . and , the american way - and I want it the European way - e.g. instead of 14.70 I want 14,70.

I made a method:

public static String getLocale(BigDecimal n) {
   String newValue      =   null;
   if (n != null) 
      newValue  =   NumberFormat.getInstance().format(n);   
   return newValue;
}

Wherein when I print the entire ArrayList to a .csv I call this method for each of the Bigdecimals. However I was wondering if I can change the entire ArrayList to know, that I want to store elements the "correct" way for me? Or maybe to change the Locale when I set the BigDecimal into the ArrayList?

What is the best way to do this?


Solution

  • The best way is the way you've done it, although I would name the method format() instead of getLocale(), since the method doesn't get a Locale, but formats a BigDecimal.

    The data is a list of BigDecimals. Whether and how this data is displayed/stored in a CSV file shouldn't change the nature of the data. If later, you need to display these data in a web page in English, you shouldn't have to change the nature of the data, but only the way this data is displayed.