Home> Java> JavaBase> body text

How to convert string to number in java

coldplay.xixi
Release: 2020-11-02 09:46:02
Original
76788 people have browsed it

Java string to number method: 1. Convert to integer number [Integer.parseInt(String s)]; 2. Convert to floating point number [Float.parseFloat(String s)].

How to convert string to number in java

Related free learning recommendations:java basic tutorial

java string Method of converting numbers:

1. Convert to integer numbers

(1) Integer.parseInt(String s), the code example is as follows:

public class Test { public static void main(String args[]){ String s = "123"; int num = Integer.parseInt(str); int sum = num + 100; System.out.println("Result is: "+sum); // 输出结果为:Result is: 223 }}
Copy after login

How to convert string to number in java

(2) Integer.valueOf(String s), the code example is as follows:

public class Test2 { public static void main(String args[]){ String s = "-100"; int num = Integer.valueOf(str); int sum = num + 101; System.out.println("Result is: "+sum); // 输出结果为:Result is: 1 } }
Copy after login

How to convert string to number in java

2, Convert to floating point number

(1) Float.parseFloat(String s), the code example is as follows:

public class Test { public static void main(String args[]){ String s = "123.11"; float num = Float.parseFloat(s); float sum = num + 100; System.out.println("Result is: "+sum); // 输出结果为:Result is: 223.11 }}
Copy after login

How to convert string to number in java

(2) Double .parseDouble(String s), the code example is as follows:

public class Test2 { public static void main(String args[]){ String s = "100.01"; double num = Double.parseDouble(s); double sum = num + 100; System.out.println("Result is: "+sum); // 输出结果为:Result is: 200.01 }}
Copy after login

How to convert string to number in java

The specific code required in the question to convert String s="00000123" into 123 is as follows:

public class Test { public static void main(String args[]){ String s = "00000123"; int num = Integer.parseInt(s); System.out.println("Result is: "+num); // 输出结果为:Result is: 123 }}
Copy after login

How to convert string to number in java

Extended information:

1. Convert integer and floating point types to strings in Java:

public class Test { public static void main(String args[]){ int i = 11; String s = i + ""; // 方法一 String s = String.valueOf(i); // 方法二 String s = Integer.toString(i); // 方法三 } }
Copy after login

How to convert string to number in java

2. Determine whether a string is a number in Java:

public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; }
Copy after login

How to convert string to number in java

The above is the detailed content of How to convert string to number in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!