新知识-valueOf(Leetcode 1556_千位分隔符)
新知识-valueOf(Leetcode 1556_千位分割符)
1556.千位分隔数
代码
class Solution {
public String thousandSeparator(int n) {
StringBuilder sb = new StringBuilder(String.valueOf(n));
for(int i = sb.length()-3; i > 0; i -= 3){
sb.insert(i,'.');
}
return new String(sb);
}
}
valueOf
- valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类型, String等
int -> String
int n = 9;
String s = String.valueOf(n);
System.out.print(s);
.补充
int -> char
int n = 9;
char c = (char)(n + 48);
Integer -> String
Integer c = 9;
String s = c.toString();