1556. 千位分隔数

给你一个整数 n,请你每隔三位添加点(即 "." 符号)作为千位分隔符,并将结果以字符串格式返回。

示例 1:

输入:n = 987
输出:"987"

示例 2:

输入:n = 1234
输出:"1.234"

示例 3:

输入:n = 123456789
输出:"123.456.789"

示例 4:

输入:n = 0
输出:"0"

提示:

  • 0 <= n < 2^31
import java.util.ArrayList;

public class Solution1556 {

	public String thousandSeparator(int n) {
		String out = "";
		String temp = String.valueOf(n);
		for (int i = temp.length() - 1; i > -1; i--) {
			out = temp.substring(i, i + 1) + out;
			if ((temp.length() - i) % 3 == 0 & i != 0) {
				out = "." + out;
			}
		}

		return out;

	}

	public static void main(String[] args) {

		Solution1556 sol = new Solution1556();

		int n = 1234;
		System.out.println(sol.thousandSeparator(n));
	}
}

猜你喜欢

转载自blog.csdn.net/allway2/article/details/114106608