java代码不能格式化 java数字格式化异常

java中时间格式化和常规类型格式化format总出错Date d_date = new Date();
int year = new Integer(String.format("%tY", d_date));
int month = new Integer(String.format("%tm", d_date));
int day = new Integer(String.format("%td", d_date));
JAVA程序中,关于格式化输出的问题"%-50"的意思是如果字符串小于50长度右边的补空格,很明显你的50太长了 把姓名这一列挤的太靠后了
JAVA里面如何格式化数字楼主你好!给你写了个测试类希望能帮助你 。这两个个方法只需要传入你要格式话的数据,就可以返回你想要的结果了 。package com.line;public class T9 {
/**
* b格式化一般数据为财务格式 , eg:123,456,789.00/b
*
* @param source
*String
* @return String
*/
public static String getCaiWuData(String source) {
StringBuffer str = new StringBuffer("");
if (source != null!source.equals("")source.length()0
!source.equals("null")) {
if (source.lastIndexOf(",")0) {
source =formatStr(source);
}
int dotIndex = 0;
if (source.indexOf(".")0) {
source += ".00";
}
dotIndex = source.indexOf(".");
int index = 0;
String opt = "";
opt = source.substring(0, 1);
if (opt.equals("-")) {
source = source.substring(1);
str.append("-");
dotIndex = source.indexOf(".");
}
if (dotIndex3) {
index += 1;
str.append(source.substring(0, dotIndex));
}
if (dotIndex % 3 == 0) {
index += dotIndex / 3;
} else {
index += (dotIndex - dotIndex % 3) / 3;
}
if (index0dotIndex = 3) {
for (int i = index; i0; i--) {
if (i == index) {
str.append(source.substring(0, dotIndex - i * 3));
}
if (dotIndex - i * 30) {
str.append(",");
}
if (i = 1) {
str.append(source.substring(dotIndex - i * 3, dotIndex
- (i - 1) * 3));
}
}
}
str.append(source.substring(dotIndex));
}
if (source.length() - source.lastIndexOf(".")3) {
str.append("0");
}
int dot_index = str.toString().indexOf(".") + 2;
int str_len = str.toString().length();
【java代码不能格式化 java数字格式化异常】char[] strArr = str.toString().toCharArray();
StringBuffer rev = new StringBuffer();
for (int i = str_len - 1; i0; i--) {// 除去尾数0,小数点后保留2位
if (idot_index
Integer.parseInt(new Character(strArr[i]).toString())0) {
rev.append(str.toString().substring(0, i + 1));
break;
} else if (i == dot_index(int) strArr[i] = 0) {
rev.append(str.toString().substring(0, dot_index + 1));
break;
}
}
return rev.toString();
}
/**
* b格式化财务数据为一般字符串,eg:123456789.00/b
*
* @param source
*String
* @return String
*/
public static String formatStr(String source) {
StringBuffer str = new StringBuffer("");
if (source != null!source.equals("")source.length()0
!source.equals("null")) {
String temp = source.substring(0, 1);
if (temp.equals("-")) {
source = source.substring(1);
str.append("-");
}
String[] myarr = source.split(",");
int lastIndex = source.lastIndexOf(",");
if (lastIndex0) {
for (int i = 0; imyarr.length; i++) {
str.append(myarr[i]);
}
}
if (source.lastIndexOf(",")0) {
str.append(source);
}
if (source.lastIndexOf(".")0) {
str.append(".00");
}
if (source.length() - source.lastIndexOf(".")3
!"0".equals(source)) {
str.append("0");
}
} else {
return (str.append("0.00").toString());
}
return str.toString();
}
/**
* @param args
*/
public static void main(String[] args) {

推荐阅读