Removing Dollar and comma from string

点点圈 提交于 2019-11-29 02:01:35

do like this

NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("$123,456.78");
System.out.println(number.toString());

output

123456.78

Try,

String liveprice = "$123,456.78";
String newStr = liveprice.replaceAll("[$,]", "");

replaceAll uses regex, to avoid regex than try with consecutive replace method.

 String liveprice = "$1,23,456.78";
 String newStr = liveprice.replace("$", "").replace(",", "");

Just use Replace instead

String liveprice = "$123,456.78";
String output = liveprice.replace("$", "");
output = output .replace(",", "");

Without regex, you can try this:

String output = "$123,456.78".replace("$", "").replace(",", "");

In my case, @Prabhakaran's answer did not work, someone can try this.

String salary = employee.getEmpSalary().replaceAll("[^\\d.]", "");
Float empSalary = Float.parseFloat(salary);

Here is more information Oracle JavaDocs:

liveprice = liveprice.replace("X", "");

Is a replace really what you need?

public void test() {
  String s = "$123,456.78";
  StringBuilder t = new StringBuilder();
  for ( int i = 0; i < s.length(); i++ ) {
    char ch = s.charAt(i);
    if ( Character.isDigit(ch)) {
      t.append(ch);
    }
  }
}

This will work for any decorated number.

Will this works?

String liveprice = "$123,456.78";
String newStr = liveprice.replace("$", "").replace(",","");

Output: 123456.78

Live Demo

Better One:

String liveprice = "$123,456.78";
String newStr = liveprice.replaceAll("[$,]", "")

Live Demo

Example using Swedish Krona currency

String x="19.823.567,10 kr";

        x=x.replace(".","");
        x=x.replaceAll("\\s+","");
        x=x.replace(",", ".");
        x=x.replaceAll("[^0-9 , .]", "");

System.out.println(x);

Will give the output ->19823567.10(which can now be used for any computation)

Bruno Lopes Malafaia

I think that you could use regex. For example:

"19.823.567,10 kr".replace(/\D/g, '')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!