This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
How to do an Integer.parseInt() for a decimal number?
(10 answers)
Closed 9 years ago.
How to convert string value into int? I am getting number format exception.
String s = "20.00";
int i = (Integer.parseInt(s));
System.out.println(i);
Result should be like i=20.
What about:
int i = (int) Double.parseDouble(s);
Of course, "20.00" is not in a valid integer format.
String s = "20.00";
is not valid Integer value that is the reason its throwing NumberFormatException.
Format your number using either Double or Float then using narrow casting cast you number to int but you may loose precision if exists.
i.e. int I = (int) Double.parseDouble(str);
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I want to convert the string to float with out losing it's precision.
String str = "140000.01";
float val = Float.valueOf(str);
Here, I'm getting the output i.e float value as 140000.02, is there any way to convert it into float without losing precision.
Simple way you can use double, like this:
String str = "140000.01";
double val = Double.parseDouble(str);
This question already has answers here:
How do I get the part after the decimal point in Java?
(15 answers)
Closed 6 years ago.
int number = 24.24;
int afterDot = (int) (number*100)%100;
afterDot = 24
This logic is wrong if
int number1 = 24.4
How can I get the 4 from 24.4 ?
Whatever will be the number but want to extract the value after dot.
Actually the formula has to work for both type of value.
If you convert your double to a string, the problem becomes easier :
double number = 24.4;
String numberAsString = String.valueOf(number);
String decimalPart = numberAsString.split("\\.")[1];
System.out.println(decimalPart);
int number1 = Integer.valueOf(decimalPart); // NOTE: This conversion is lossy.
System.out.println(number1);
Note that by converting your decimalPart string (e.g. "001") to an integer (1), you might lose some information.
With 24.4, it outputs :
4
4
With 24.001, it outputs :
001
1
With 3d, it outputs:
0
0
This question already has answers here:
java: convert binary string to int
(4 answers)
Closed 6 years ago.
there is a string fill with 0 and 1,like String s = "10000000", which length is 8.And how can i transform it to a byte.such as "10000000"===>-128.
I try to use Byte.parseByte(s, 2),but get the error "Value out of range. Value:"10000000" Radix:2".So,how can i solve it.
You need to parse it as an Integer and then cast it to byte:
...
String s = "10000000";
int val = Integer.parseInt(s, 2);
byte b = (byte) val;
System.err.println(b);
...
Output:
-128
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 7 years ago.
I'm having problems when taking a value out of a list and then casting it as an integer so I then can use it for different math functions, such as multiplication.
Current code :
int i = 0;
while(i < student_id.size()){
String finding = student_id.get(i).toString();
int s101 = ((Integer)score101.get(student101.indexOf(finding))); // <----this is where im having problems
System.out.println(student_id.get(i)+" "+names.get(i));
System.out.println("IR101 " +
score101.get(student101.indexOf(finding)) +
" IR102 " +
score102.get(student102.indexOf(finding)));
i++;
}
The error that im getting is java.lang.String cannot be cast to java.lang.Integer. This confuses me because I thought it would have been an object. I have tried to convert it to an integer from both an object and a String but both throw up errors. How can I convert score101.get(student101.indexOf(finding)) to an int ?
The error is pretty clear, java.lang.String cannot be cast to java.lang.integer.
That means score101.get(student101.indexOf(finding)) return a String. If the string represent an Integer then you can parse it easily
Integer.parseInt(score101.get(student101.indexOf(finding)))
Edit
As per your comment, the string is a Double so you need to use parseDouble
Double.parseDouble(score101.get(student101.indexOf(finding)))
If you really want it as an int and discard the decimal, you can call intValue() which will cast it to an int (or cast directly).
Double.parseDouble(score101.get(student101.indexOf(finding))).intValue()
You can parse string with Integer.parseInt(String s) method
This question already has answers here:
How to convert float to int with Java
(9 answers)
Closed 7 years ago.
I need to convert a float number into an integer.
Can Java automatically convert float number into integers? If so, do normal rounding rules apply (e.g. 3.4 gets converted to 3, but 3.6 gets converted to 4)?
You have in Math library function round(float a) it's round the float to the nearest whole number.
int val = Math.round(3.6); \\ val = 4
int val2 = Math.round(3.4); \\ val2 = 3
It is a bit dirty but it works:
double a=3.6;
int b = (int) (a + 0.5);
See the result here
Math.round(3.6) would do that for you.