java字符串操作
请写一个方法吧一个输入整数转化为经过加入千分隔符处理操作的字符串。例如:如果输入1234,返回1,234. 如果输入1234567,返回1,234,567. 但是如果输入12 返回12. 不能使用java中的 formatter java --------------------编程问答--------------------package com.example.learnjava.Common;
import java.io.*;
public class HuiWen {
public static void main(String[] args) throws IOException {
System.out.print("请你输入整数:");
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(buf.readLine());
int j = String.valueOf(i).getBytes().length;
int s[] = new int[j / 3 + 1];
for (int k = 0; k < j / 3 + 1; k++) {
s[k] = i % 1000;
i = i / 1000;
}
for (int k = j / 3; k >= 0; k--) {
if (k != 0) {
System.out.print(s[k] + ",");
}
else if (k == 0) {
System.out.print(s[k]);
}
}
}
}
输入的整数不能过大 --------------------编程问答--------------------
若个数能被3整除,会多个0,要断网了,不改了。。。 --------------------编程问答--------------------
public class Formatter {--------------------编程问答-------------------- sorry贴错版本了
public static void main(String[] args) {
String[] tests = new String[] {
"1",
"12",
"123",
"1234",
"12345",
"123456",
"1234567",
"12345678",
"123456789",
"1.1",
"12.12",
"123.123",
"1234.1234",
"12345.12345",
"123456.123456",
"1234567.1234567",
"12345678.12345678",
"123456789.123456789"
};
for (String test : tests) {
System.out.printf("%s\t[%s]%n", test, format(test));
}
}
public static String format(String numStr) {
if (numStr == null) {
return "";
}
char[] chars = numStr.toCharArray();
int decimal = 0;
for (; decimal < chars.length; decimal++) {
if (chars[decimal] == '.') {
break;
}
}
char[] formatted = new char[chars.length + Math.max((decimal - 1) / 3, 0)];
for (int i = 0, pos = 0; i < chars.length; i++) {
if (i != 0 && i % 3 == 0 && i < decimal) {
formatted[pos++] = ',';
}
formatted[pos++] = chars[i];
}
return new String(formatted);
}
}
public class Formatter {--------------------编程问答-------------------- 当然,这个版本没有考虑负数情况。如果需要支持负数,多考虑一位即可。
public static void main(String[] args) {
String[] tests = new String[] {
"1",
"12",
"123",
"1234",
"12345",
"123456",
"1234567",
"12345678",
"123456789",
"1.1",
"12.12",
"123.123",
"1234.1234",
"12345.12345",
"123456.123456",
"1234567.1234567",
"12345678.12345678",
"123456789.123456789"
};
for (String test : tests) {
System.out.printf("%s\t[%s]%n", test, format(test));
}
}
public static String format(String numStr) {
if (numStr == null) {
return "";
}
char[] chars = numStr.toCharArray();
int decimal = 0;
for (; decimal < chars.length; decimal++) {
if (chars[decimal] == '.') {
break;
}
}
char[] formatted = new char[chars.length + Math.max((decimal - 1) / 3, 0)];
for (int i = 0, pos = 0; i < chars.length; i++) {
if (i != 0 && i % 3 == decimal % 3 && i < decimal) {
formatted[pos++] = ',';
}
formatted[pos++] = chars[i];
}
return new String(formatted);
}
}
如果不需要支持小数,去掉前面那个for,直接将decimal替换成chars.length即可 --------------------编程问答--------------------
--------------------编程问答-------------------- 转换成字符串之后反向遍历即可
/** 100000000
*100,000,000
*/
public static BigDecimal getValueComma(String val, int newScale) {
BigDecimal result = null;
if (null != val && !"".equals(val)) {
val = val.replace(",", "");
result = new BigDecimal(val).setScale(newScale,
BigDecimal.ROUND_HALF_UP);
}
return result;
}
public static void main(String[] args){
int a = 123523456;
StringBuilder str = new StringBuilder(String.valueOf(a));
int len = str.length();
for(len -= 3; len > 0; len -= 3){
str.insert(len, ',');
}
System.out.print(str);
}
--------------------编程问答-------------------- 4楼 --------------------编程问答--------------------
+!
补充:Java , Java EE