Get the App
SLTechnology News&Howtos  ›  Development  › 

How to change strings into numbers in java

Shulou Source: shulou.com Published: 2022-06-01 12:53:33 10月25日 Update

In this article Xiaobian for you to introduce in detail "how to turn a string into a number in java", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to turn a string into a number in java" can help you solve your doubts.

Method 1:

Integer.parseInt ():

This method, we are through the character type of a number into an integer process, let's look at the code and run a result!

Package Change;public class TrunChange {public static void main (String [] args) {String s = "0000012342"; int a = Integer.parseInt (s); System.out.println ("before the string is:" + s); System.out.println ("the converted number is:" + a);}}

As we can see in the results, the string before conversion is different from the comparison after conversion. And the usage of Integer.valueOf () is the same.

Method 2:

Float.parseFloat ():

If we use this method, we convert a numeric string into a floating-point number, the code and the result are as follows:

Package Change;public class TrunChange {public static void main (String [] args) {String s = "0000012342"; float b=Float.parseFloat (s); System.out.println ("before the string is:" + s); System.out.println ("the converted number is:" + b);}}

The number shown in this result is followed by a decimal point, which shows that our results are correct.

After reading this, the article "how to turn a string into a number in java" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, please follow the industry information channel.

Tags: Number character string result method article code content run proper character type decimal point idea new knowledge clear more step floating point knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux Shulou Technology NVidia macOS