Java把int型数字转换为string型
Posted by 撒得一地 on 2016年4月19日 in 杂谈
上一篇: 求最小公倍数和最大公约数的各种方法
下一篇: 埃氏筛法求素数 java 代码
下一篇: 埃氏筛法求素数 java 代码
public class IntToString {
/**
* Algorithm implementation based on the module of a given number and
* iterate until the number is zero. With this approach we get a complexity
* order equals to O(N) in time terms where N is the number of digits in the
* given integer. In space terms, the complexity order is also O(N) because
* we are using an StringBuilder as auxiliary data structure.
*/
public String transform(int number) {
boolean isNegative = false;
StringBuilder sb = new StringBuilder();
if (number == 0) {
return "0";
} else if (number < 0) {
isNegative = true;
}
number = Math.abs(number);
while (number > 0) {
sb.append(number % 10);
number /= 10;
}
if (isNegative) {
sb.append("−");
}
return sb.reverse().toString();
}
}
上一篇: 求最小公倍数和最大公约数的各种方法
下一篇: 埃氏筛法求素数 java 代码
下一篇: 埃氏筛法求素数 java 代码