Live2D

新知识-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();

Leetcode学习打卡100+

posted @ 2021-08-11 18:05  饭耶  阅读(193)  评论(0编辑  收藏  举报