-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
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Stillie
  • 2,647
  • 6
  • 28
  • 50

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
  • 569
  • 4
  • 18
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
  • 74,896
  • 15
  • 165
  • 198
2

You could use a DecimalFormat

import java.text.*;

NumberFormat nf = new DecimalFormat("$0.00");
int value = (int) nf.parse("$16500.00");
lance-java
  • 25,497
  • 4
  • 59
  • 101
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