Convert numeric Strings into integer, float, double values using parse method
Integer.parseInt() , Double.parseDouble() , Float.parseFloat() methods
In this video, I would like to show you how to convert Numeric Strings into actual numbers.
By definition, a numeric string is a String value consisting of only an Integer or a decimal number. For example these Strings on the screen are all made up of only integer or decimal numbers String str1 = “-100”; String str2 = “140.50”; or String str3 = “28.69”;
If you try to run arithmetic operations on these variables, you will not get the expected result.
Because these are actually Strings, the plus + symbol between them is considered as a concatenation operator but not an addition operator. Using the other arithmetic operators on these variables will result in an error.
So, in Java we can use special methods to convert these numeric Strings into actual numbers of type Integer, floating-point or Double; so that we can be able to perform arithmetic operations on them
Let me take the first example to show you how to convert a numeric string into an Integer; and for that matter I am going to use the Integer.parseInt() method.
Since that method will return an integer, I then need to declare an integer variable and call the method
int num1 = Integer.parseInt(str1);
System.out.println(num1);
Now, if I perform an arithmetic operation on this value, it will work
System.out.println(num1 + 10);
In the second example, I am going to convert into a value of type Double. So, I will follow the same logic
double num2 = Double.parseDouble(str2);
System.out.println(num2);
Finally, in the last example I am going to convert into a value of type Float
float num3 = Float.parseFloat(str3);
System.out.println(num3);
Now, you can freely perform arithmetic operations on these converted values, as you wish.
Thanks for viewing, I hope this video was informative and please do not forget to Subscribe to this channel for more
Nesta página do site você pode assistir ao vídeo on-line Convert numeric Strings into integer, float, double values using parse method duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Career & Tech HQ 24 Junho 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 11,209 vezes e gostou 163 espectadores. Boa visualização!