type conversion


int to String

有三种方法,其中第二种第三种效率差不多,要比第一种更快。

a+""
String.valueOf(a)
Integer.toString(a)

String to int 

int a = Integer.parseInt(str);

int to char

最正确的方式:

char c = String.valueOf(5).charAt(0);

简便方法:

char c  = (char) ('0' + 5);

char to int 

最正确的方式:

char c =5;
String str = String.valueOf(c);
int a  = Integer.parseInt(str);

简便方法:

char charNum = '5';
int num = char - '0';

double to long

Long l = new Double(3.0).longValue();

long to double

long l = 2L;
Double d = l.doubleValue();

其他

如果long或者double出现溢出,需要使用BigInteger和BigDecimal。引入import java.math.BigInteger(BigDecimal)。

使用方法如下:

BigInteger b = BigInteger.valueOf(a);
或者BigInteger b = new BigInteger(String.valueOf(str));

相加是b.add(a);


Author: csy99
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source csy99 !
评论
 Previous
r r
排序对数据集进行排序的时候,不打乱行之间的联系#sort m by using the order and keep the connections.#Don’t miss the comma after the order.sort_df
2020-04-01
Next 
sql sql
General Hints Case insensitive, except folders in Linux Double quote and single quote does not matter, prefer single qu
2020-02-16
  TOC