Java-String和int、double,char的互相转换、int转BigInteger

1、String转int

int res = Integer.parseInt(s);

package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;

import javax.naming.StringRefAddr;

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner sc = new Scanner(System.in);
         final int N = 10;
         
         String s;
         s = sc.nextLine();
         
         int res = Integer.parseInt(s);
         System.out.printf("%d\n",res);
    }
}

2、String转double

double res = Double.parseDouble(s);

package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;

import javax.naming.StringRefAddr;

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner sc = new Scanner(System.in);
         final int N = 10;
         
         String s;
         s = sc.nextLine();
         
         double res = Double.parseDouble(s);
         System.out.printf("%f",res);
    }
}

3、int转String

(1)s = "" + n;

package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;

import javax.naming.StringRefAddr;

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner sc = new Scanner(System.in);
         final int N = 10;
         
         String s;
         int n = sc.nextInt();
         
         s = "" + n;
         
         System.out.println(s);
    }
}

(2)用Integer的API-toString

import java.io.*;
import java.math.BigInteger;
import java.util.*;


public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e5 + 10;

    public static void main(String[] args) throws NumberFormatException, IOException
    {
        Integer n = rd.nextInt();
        String str = n.toString();
        pw.println(str);

        pw.flush();
    }
}

class MyComparator implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII implements Comparable<PII>
{
    long x,y;
    public PII(long x ,long y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.y-a.y != 0)
            return Math.toIntExact(this.y - a.y);  //按x升序排序
        else return Math.toIntExact(this.x - a.x);  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

4、double转String

s = "" + n;

package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;

import javax.naming.StringRefAddr;

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner sc = new Scanner(System.in);
         final int N = 10;
         
         String s;
         double n = sc.nextDouble();
         
         s = "" + n;
         
         System.out.println(s);
    }
}

5、char转String

s = "" + n;

package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;

import javax.naming.StringRefAddr;

public class Main 
{
    public static void main(String[] args) 
    {
         Scanner sc = new Scanner(System.in);
         final int N = 10;
         
         String s;
         char n = sc.nextLine().charAt(0);
         
         s = "" + n;
         
         System.out.println(s);
    }
}

6、int转BigInteger

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.function.BiFunction;


public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e5 + 10;

    public static void main(String[] args) throws NumberFormatException, IOException
    {
        int n = rd.nextInt();

        //方法2:主打万能简洁
        BigInteger res1 = BigInteger.valueOf(n);

        // 方法2:主打原始
        String s = "" + n;
        BigInteger res2 = new BigInteger(String.valueOf(s));

        pw.println(res1);
        pw.println(res2);
        pw.flush();
    }
}

class MyComparator implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII implements Comparable<PII>
{
    long x,y;
    public PII(long x ,long y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.y-a.y != 0)
            return Math.toIntExact(this.y - a.y);  //按x升序排序
        else return Math.toIntExact(this.x - a.x);  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值