Skip to main content
3 of 5
added 2 characters in body
eatSleepCode
  • 327
  • 1
  • 2
  • 9

Optimization string related code

I have written this block its working fine but is it optimized? I am getting currency in format 1.234,25 .

public static String convertCurrency(String value, String exchangeCurrencyId)
{
    java.text.NumberFormat format = null;
    value = value.replaceAll("\\.", ",");
    char[] chars = value.toCharArray();
    chars[value.lastIndexOf(',')] = '.';
    value = new String(chars);
    value = value.replaceAll(",", "");
    Double price = Double.valueOf(value);
    price = price * (50); //exchange rate

    return price.toString();
}

EDIT

I have removed some unnecessary changes, with the help of following code I can avoid a loop

    value = value.replaceAll("\\.", ",");
    char[] chars = value.toCharArray();
    chars[value.lastIndexOf(',')] = '.';
    value = new String(chars);
    value = value.replaceAll(",", "");
    Double price = Double.valueOf(value);
eatSleepCode
  • 327
  • 1
  • 2
  • 9