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
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);
You could use a DecimalFormat
import java.text.*;
NumberFormat nf = new DecimalFormat("$0.00");
int value = (int) nf.parse("$16500.00");
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
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);
Related
I'm trying to parse a string to an integer. The string is a number when I do sys out but when I try to parse it then it adds a double quote at the beginning. I'm not doing anything additional.
Following is a snippet of my code:
System.out.println("Result 2 is: "+results[2]);
String temp = results[2].replace("\"", "");
System.out.println("String Temp is: "+temp);
int height = Integer.parseInt(results[1].replace("\"", ""));
int width = Integer.parseInt(results[2].replace("\"", ""));
In the logs:
String Temp is: 2550
wt.system.err wcadmin - java.lang.NumberFormatException: For input string: "2550
This exception is only for result[2]. Something to do with character set?
Option 1: Parse temp. Change
int width = Integer.parseInt(results[2].replace("\"", ""));
to
int width = Integer.parseInt(temp);
Option 2: Use a regular expression to remove everything not a digit instead of trying to handle corner cases by hand.
int width = Integer.parseInt(results[2].replaceAll("\\D+", ""));
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?
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);
string sourceString = "$1,076.00";
sourceString.substring(1, sourceString.length() - 1)
int foo = Integer.parseInt(sourceString);
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
String originalAmount="$1076.00";
String amount = originalAmount.replace($,"");
int edited = Integer.parseInt(amount);
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
I am selecting an item from arraylist
ArrayAdapter.getItem(which)
I am getting the result as Amazon.com%2C%20Inc.-AMZN
I just want the last part that is AMZN of this string to append in my url.
Normal String operation like lastIndexOf and substring
String str = "Amazon.com%2C%20Inc.-AMZN";
String result = str.substring(str.lastIndexOf("-") + 1);
System.out.println(result);
or if there are no other - in the String use
String endBit = str.split ("-")[1];
Please refer this url
How to get a string between two characters?
String s = "Amazon.com%2C%20Inc.-AMZN";
String result = s.substring(s.indexOf("-") + 1);
System.out.println(result);
Output: AMZN
try this.
if there is no other -
String val = "Amazon.com%2C%20Inc.-AMZN";
String arr[]=val.split("-");
and at index 1 you can get your value.
like arr[1]// this will contain "AMZN"
I'm getting the error "cannot convert from String to Double" when I run this code, any ideas?
DecimalFormat df = new DecimalFormat("##.###");
String [] data = null;
data = str.split(" ");
Double bat_avg = Double.parseDouble(data[4])/Double.parseDouble(data[2]);
bat_avg = df.format(bat_avg);
System.out.println(bat_avg);
DecimalFormat#format(double) returns a String so the output needs to be this type rather than a Double:
String output = df.format(bat_avg);
I just created sample BB app, which can allow to choose the date.
DateField curDateFld = new DateField("Choose Date: ",
System.currentTimeMillis(), DateField.DATE | DateField.FIELD_LEFT);
After choosing the date, I need to convert that long value to String, so that I can easily store the date value somewhere in database.
I am new to Java and Blackberry development.
long date = curDateFld.getDate();
How should I convert this long value to String? Also I want to convert back to long from String. I think for that I can use long l = Long.parseLong("myStr");?
See the reference documentation for the String class: String s = String.valueOf(date);
If your Long might be null and you don't want to get a 4-letter "null" string, you might use Objects.toString, like: String s = Objects.toString(date, null);
EDIT:
You reverse it using Long l = Long.valueOf(s); but in this direction you need to catch NumberFormatException
String strLong = Long.toString(longNumber);
Simple and works fine :-)
Long.toString()
The following should work:
long myLong = 1234567890123L;
String myString = Long.toString(myLong);
very simple,
just concatenate the long to a string.
long date = curDateFld.getDate();
String str = ""+date;
1.
long date = curDateFld.getDate();
//convert long to string
String str = String.valueOf(date);
//convert string to long
date = Long.valueOf(str);
2.
//convert long to string just concat long with empty string
String str = ""+date;
//convert string to long
date = Long.valueOf(str);
String logStringVal= date+"";
Can convert the long into string object, cool shortcut for converting into string...but use of String.valueOf(date); is advisable
Just do this:
String strLong = Long.toString(longNumber);
String longString = new String(""+long);
or
String longString = new Long(datelong).toString();