欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

各种类型转换全家桶这(一篇就够了)

程序员文章站 2022-03-26 15:48:34
String类型String转int :String a="6666"; Integer a1 = Integer.parseInt(a); 或 int a1 = Integer.parseInt(a);String转LongString a = “6666”;Long a = Long.valueOf(a) // 返回Long包装类型long a1 = Long.parseLong(a) // 返回long基本数据类型String转Double注意转换精度问题 使用BigDecim...

String转int (int转String):

String转int :
String a="6666";
 Integer a1 = Integer.parseInt(a); 或  int a1 = Integer.parseInt(a);
int转String :

int a=12;
String a1 = String.valueOf(a);
String a2 = a+"";

String转Long (Long转String)

String转Long :

String a = “6666”;
Long a = Long.valueOf(a) // 返回Long包装类型
long a1 = Long.parseLong(a) // 返回long基本数据类型

Long转String :

long a1 = 12;
String s1 = a1 + “”; // 法1:直接加空串

long a2 = 34;
String s2 = String.valueOf(a2); // 法2:String.valueOf()

long a3 = 56;
String s3 = Long.toString(a3); // 法3:Long.toString()

String转Double (Double转String)

String转Double :

注意转换精度问题 使用BigDecimal
String a=“6666000000”;

  double aa=Double.parseDouble(a);

  BigDecimal beichushu=new BigDecimal(aa);

  BigDecimal chushu=new BigDecimal(100000000); //根据你想得到的是多大的数 来除以多少个0

  BigDecimal result=beichushu.divide(chushu,new MathContext(4));//MathConText(4)表示结果精确4位!

// 2
String toBeDouble =“200.20”;
Double fromString = new Double(toBeDouble);

Double转String :

Double toBeString = 400.40;
String fromDouble = “” + toBeString;
String strDouble = String.valueOf(toBeString);
String stringDouble = toBeString.toString();
String convertedString = String.format(“%。3f”,toBeString);

String 转list

String[] strs = {“qq”,“ww”,“ee”};
List strsToList1= Arrays.asList(strs);

String[] strs = {“qq”,“ww”,“ee”};
List strsToList2=new ArrayList<>();
Collections.addAll(strsToList2,strs);

String[] strs = {“qq”,“ww”,“ee”};
List strsToList3=new ArrayList<>();
for(String s:strs){
strsToList3.add(s);
}

String 转map 以及 map转String


public class Test05 {
 
	public static void main(String[] args) {
		
		Map<String,Object> map = new HashMap<>(3);
		map.put("name","小明");
		map.put("sex","男");
		map.put("age","16");
        //Map转String
        String str1 = getMapToString(map);
        System.out.println(str1);
        try {
           //String转map
           Map<String, Object> map1 = getStringToMap(str1);
           System.out.println(map1);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	/**
    *
    * Map转String
    * @param map
    * @return
    */
   public static String getMapToString(Map<String,Object> map){
       Set<String> keySet = map.keySet();
       //将set集合转换为数组
       String[] keyArray = keySet.toArray(new String[keySet.size()]);
       //给数组排序(升序)
       Arrays.sort(keyArray);
       //因为String拼接效率会很低的,所以转用StringBuilder
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < keyArray.length; i++) {
           // 参数值为空,则不参与签名 这个方法trim()是去空格
           if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
               sb.append(keyArray[i]).append(":").append(String.valueOf(map.get(keyArray[i])).trim());
           }
           if(i != keyArray.length-1){
               sb.append(",");
           }
       }
       return sb.toString();
   }
   
   /**
    * 
    * String转map
    * @param str
    * @return
    */
   public static Map<String,Object> getStringToMap(String str){
       //根据逗号截取字符串数组
       String[] str1 = str.split(",");
       //创建Map对象
       Map<String,Object> map = new HashMap<>();
       //循环加入map集合
       for (int i = 0; i < str1.length; i++) {
           //根据":"截取字符串数组
           String[] str2 = str1[i].split(":");
           //str2[0]为KEY,str2[1]为值
           map.put(str2[0],str2[1]);
       }
       return map;
   }
}

本文地址:https://blog.csdn.net/nanZhaiXiaoLang/article/details/113929638