18

How can we remove dollar sign ($) and all comma(,) from same string? Would it be better to avoid regex?

String liveprice = "$123,456.78";
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Varun Vishnoi
  • 980
  • 1
  • 9
  • 32

11 Answers11

33

do like this

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

output

123456.78
Rahul Mahadik
  • 11,668
  • 6
  • 41
  • 54
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
26

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(",", "");
Masudul
  • 21,823
  • 5
  • 43
  • 58
3

Without regex, you can try this:

String output = "$123,456.78".replace("$", "").replace(",", "");
Prateek
  • 6,785
  • 2
  • 24
  • 37
2

Here is more information Oracle JavaDocs:

liveprice = liveprice.replace("X", "");
Faraday
  • 2,904
  • 3
  • 23
  • 46
2

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

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
2

Just use Replace instead

String liveprice = "$123,456.78";
String output = liveprice.replace("$", "");
output = output .replace(",", "");
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
2

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

String salary = employee.getEmpSalary().replaceAll("[^\\d.]", "");
Float empSalary = Float.parseFloat(salary);
minato
  • 2,028
  • 1
  • 18
  • 30
  • While this does work, some might criticize regEx as being slow and tricky. Of course, as we see, figuring out lib calls and options is tricky also. An all-purpose powerful lib, is in many cases an intricate puzzle that seems unable to cover the problem at-hand. – MarkHu Sep 17 '21 at 23:16
0

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.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

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)

0
import java.text.NumberFormat

def currencyAmount = 9876543.21 //Default is BigDecimal
def currencyFormatter = NumberFormat.getInstance( Locale.US )

assert currencyFormatter.format( currencyAmount ) == "9,876,543.21"

Don't need getCurrencyInstance() if currency is not required.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
-1

I think that you could use regex. For example:

"19.823.567,10 kr".replace(/\D/g, '')
Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69