-5

Im having an issue trying to convert $16500.00 String to 16500 Integer.

This is what i have at the moment but its failing with:

FATAL EXCEPTION: main java.lang.NumberFormatException: Invalid int: "[]"

The code i have is:

String depTest = mDepositAmount.getText().toString();
String deptest2 = Arrays.toString(depTest.replace("R", "").replace(",", "").split("."));
int dep = Integer.parseInt(deptest2);

Please could you help me with getting the end result to 16500. I know how to convert it to int by using Integer.parseInt its just im struggling to get the end result to be 16500 in String

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
Stillie
  • 2,565
  • 5
  • 26
  • 47

4 Answers4

4

Does your original string always starts with character '$' and followed by number format?
If so try this one:

String org = "$16500.00";
String numbersOnly = org.substring(1);   // "16500.00"
int yourInteger = (int)(Float.parseFloat(numbersOnly));

// if you need String, convert it to String again
String integerString = Integer.toString(yourInteger);


Suhyeon Lee
  • 589
  • 4
  • 17
2

You can try with this

String getValue = "$16500.00";
String removeFirstCharecter = getValue.substring(1);   // "16500.00"
String [] getString = removeFirstCharecter.split("\\.");

String firstIntValue = (getString[0]); //16500
String sirstIntValue = (getString[1]); //00

Now you can convert firstIntValue String to Integer .

String getRequiredValue = Integer.toString(firstIntValue); //16500
IntelliJ Amiya
  • 73,189
  • 14
  • 161
  • 193
2

You could use a DecimalFormat

import java.text.*;

NumberFormat nf = new DecimalFormat("$0.00");
int value = (int) nf.parse("$16500.00");
lance-java
  • 23,463
  • 3
  • 50
  • 89
0

It may be happen because of $ sign so, Just take one another Text view only for Price 16500 and other for $ sign. then convert the Integer.parseInt(textview.getText().toString);

  • yea sorry, i dont know why i used the $ in the question! blonde moment, it was mean to be R. Removing the relevant text is fine, its removing the .00 that i am having an issue – Stillie Mar 09 '16 at 10:34