1556. Thousand Separator Number

Give you an integer  n, please add a dot (ie "." symbol) every third place as the thousands separator, and return the result in string format.

 

Example 1:

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

Example 2:

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

Example 3:

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

Example 4:

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

 

prompt:

  • 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));
	}
}

 

Guess you like

Origin blog.csdn.net/allway2/article/details/114106608