I want to put the string into one of the Integer/Float/Double, but the results into a NumberFormatException.. My string is: 37,78584, how to transform them for the type I want? First thank you for my answer.
3 Answers
Replace Comma(",") with ""
String s ="37,78584";
int ival = Integer.parseInt(s.replace("," ,""));
double dval=Double.parseDouble(s.replace("," ,""));
float fval =Float.parseFloat(s.replace("," ,""));
4 Comments
Ram kiran Pachigolla
see.. number format exception will arise when converting his string "37785 84" into the single number.
Ram kiran Pachigolla
@Nick if you want to convert your string into two integers then you can use 'StringTokenizer'
Lucifer
Agree with you, what I am saying is, like comma separated format , what if OP has stored the two different number with space ? Pls, see my comments on the question.
Lucifer
@Ramkiran yes now its perfect, +1 :)
You can do something like this first split your multiple number in String[] and loop over it and get the data.
String myString ="37785 84";
String[] mSplittedNumber= myString.split(" ");
int intValue = 0;
int doubleValue = 0;
int floatValue = 0f;
for(int i=0;i< mSplittedNumber.length;i++)
{
intValue =Integer.parseInt(mSplittedNumber[i]);
doubleValue =Double.parseDouble(mSplittedNumber[i]);
floatValue =Float.parseFloat(mSplittedNumber[i]);
}
Note: If your String contains multiple number with identifier(" ","|",","....etc) else above answer will do the job
3 Comments
Lucifer
OP, has one single number, so i dont think we need to split the numbers.
Mohammed Azharuddin Shaikh
Agree, but when I saw OP dint rectify his mistake so I proceed.
Lucifer
hmm... okies, no hard feelings, just to let you know that OP's req is diff, as the he forgot to add
, in the que. :)MyImplementation:
String myString = "37,78584";
{
/* Delete whitespaces from our string */
myString = myString.trim();
/* Change comma to dot cause Java double/float work with dots*/
myString = myString.replace(",", ".");
}
try {
/* Parsing */
Double myDouble = Double.valueOf(myString);
System.out.println(myDouble);
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
Hope, it was helpful for u
5 Comments
Lucifer
by replacing comma ( , ), to dot ( . ) ,dont you think it will return wrong value ?
JunR
I do not change the value, only divider to make the Double method work with new value with dot. It'll never return wrong value if there are only numbers and only one comma separator. Atherwise we should check the String for invalid format. It was the simplest answer, I think :)
Lucifer
agree, but as long as you replace , with . it will convert 3778584 to 37.78584 which is wrong it self.
Lucifer
I dont know that part, its OP who said that, but i am 100% sure , 3778584 is not equal to 37.78584
JunR
of course it's not equal!)) Maybe I didn't understand the task? :) What we had to do here? Thought, it was the number in string "37,78584"? which we had to convert into double/float? Am I right? :)
space37785 84is single number or two different numbers ?