-1

I have a string "$1,076.00" and I want to convert them in to int, I capture some value $1,076.00 and saved in string called originalAmount, and tried int edited = Integer.parseInt(originalAmount); and it gave me error java.lang.NumberFormatException: For input string: "$1,076.00"

can anyone help?

user6939
  • 363
  • 1
  • 5
  • 13
  • remove the undesired part, then parse the string to double – ΦXocę 웃 Пepeúpa ツ Mar 16 '17 at 12:06
  • you have a non numeric expression. you should remove the $ from the expression and since you manipulate double values you should parse the string as a Double. – Nemesis Mar 16 '17 at 12:09
  • what if the amount is `$1,122.33` then what should be the output because `int` won't be able to store `.33` – Pavneet_Singh Mar 16 '17 at 12:10
  • @Pavneet_Singh I only sening int value to input and it gives $by default so when I get value and save in string It will always give .00 back – user6939 Mar 16 '17 at 12:13
  • try this `int d= Integer.valueOf("$1,076.00".replaceAll("\\.0+|[^\\d]+", ""));` – Pavneet_Singh Mar 16 '17 at 12:18
  • Possible duplicate of [How to parse number string containing commas into an integer in java?](http://stackoverflow.com/questions/11973383/how-to-parse-number-string-containing-commas-into-an-integer-in-java) – JeffC Mar 16 '17 at 13:20

5 Answers5

0

You need to remove the undesired part ($ sign) and then parse the string to double carefully since the decimal part is a locale dependent

String pay = "$1,076.00";
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse(pay.replace("$", ""));
double result = number.doubleValue();
System.out.println(result);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
String originalAmount="$1076.00";
String amount = originalAmount.replace($,"");
int edited = Integer.parseInt(amount);
Pang
  • 9,564
  • 146
  • 81
  • 122
Praveen
  • 25
  • 9
0
string sourceString = "$1,076.00";
sourceString.substring(1, sourceString.length() - 1)
int foo = Integer.parseInt(sourceString);
Kalin Krastev
  • 552
  • 6
  • 19
0

Try this:

 String amount = "$1,076,.00";
 String formatted = amount.replace("$", ""); // remove "$" sign
 formatted = formatted.replace(",", ""); // remove "," signs from number

 double amountDouble = Double.parseDouble(formatted); // convert to double
 int amountInt = (int)amountDouble; // convert double value to int
 System.out.println(amountInt); // prints out 1076
Željko Krnjić
  • 2,356
  • 2
  • 17
  • 24
0

Thanks everyone yr answers help me a lot I have come up with

originalAmount = originalAmount.substring(1);

    if (originalAmount.contains("$")) {
        originalAmount = originalAmount.replace("$", "");
    }

    newOriginalAmt = Double.parseDouble(originalAmount);
    System.out.println(newOriginalAmt);

pls let me know yr thoughts

user6939
  • 363
  • 1
  • 5
  • 13