????前几天想在java中用格式化输出,多亏rollingpig、zlzj2010等几位老大提示,现在我用text package中的numberformat和decimalfomat实现了一把,给大家show一下,请多指教。
????简单说明:
????a.格式描述符:
????1. l或l - 使数字左对齐(缺省是右对齐)
????2. c或c - 使数字居中对齐(缺省是右对齐)
????3. x或x - 显示数字为十六进制(缺省是十进制)
????4. b或b - 显示数字为八进制(缺省是十进制)
????5. ,(半角逗号)- 千位分隔符
????6. s或s - 显示数字为科学记数法
????7. z或z - 用零填空位(缺省是空格)
????8. w.d (两个整数用半角句号隔开)- 指定输出域宽及精度
????b. "fillchar"是填空位符(缺省为空格)
????--你可以用myout.fillchar = '*'或'$'来定义你自己的空位符
????举例:
????println(123.45678, "8.3l") -> 123.457(左对齐占八位)
????println(123.45678, "10.3cs") -> 1.235e2 (居中占十位)
????println(1234567, ",10") -> 1,234,567(右对齐占十位)
// class myout
import java.io.printstream;
import java.io.printwriter;
import java.io.outputstreamwriter;
import java.text.decimalformat;
import java.text.numberformat;
/* thanks to prof. h.roumani.(he is my java teacher, who is great.)
*
* the format methods receive the value to be formatted (which can
* be of any type) and a format descriptor: a string that contains
* one or more of the following flags in any order and in any case:
*
* l:
* by default, all values are aligned right within their field
* width. if this flag is specified, left alignment is used instead.
* this flag has no effect if the field width is not specified.
* c:
* by default, all values are aligned right within their field
* width. if this flag is specified, centre alignment is used instead.
* this flag has no effect if the field width is not specified.
* x:
* by default, all numeric values are shown in the decimal system.
* if this flag is specified, hexadecimal is used instead (showing
* ieee-754 for real's). this flag has no effect if the value is not
* numeric.
* b:
* by default, all numeric values are shown in the decimal system.
* if this flag is specified, binary is used instead (showing
* ieee-754 for real's). this flag has no effect if the value is not
* numeric.
* ,:
* by default, all numeric, base-10 values are shown without a
* thousand-separator. if this flag is specified, a comma is inserted
* in the integer part of the number to separate thousands. this flag
* has no effect if the value is not numeric, is not in decimal, or if
* the scientific notation is used.
* s:
* by default, all numeric, base-10 values are shown as an integer part
* followed by a mantissa, or fractional part. if this flag is specified,
* scientific notation is used: one digits (possibly preceded by a minus
* sign) followed by a decimal point, a mantissa, the letter 'e' and an
* exponent. this flag has no effect if the value is not numeric or is not
* in decimal.
* z:
* by default, all integer, base-10 values are shown with leading or
* trailing spaces to fill the specified field width. if this flag is
* specified, the field is filled with leading zeros instead. this flag is
* only meaningful if the value is a base-10 integer, the width is specified,
* and the thousand-separator flag is not specified.
* w.d (two integers separated by a period):
* w is the desired width of the entire returned string, after formatting.
* if the formatted string is shorter than w, it will be padded by leading
* and/or trailing spaces (or some other fill character) depending on the
* requested alignment (left, right, or centre). d is the desired number of
* decimals and is meaningful only if the value is a base-10 real (in standard
* or scientific notation). rounding to the specified number of decimals is
* done using conventional rules but the case of 5 is handled by rounding to
* the nearest even number (same as the rint method of the math class). note
* that you can specify only w (in that case don't include the period), or
* only d (in this last case do precede it by the period).
*/
public class myout
{
/*************************************************************
formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static string format(byte value, string fd)
{
return formatinteger(value, fd, 8);
}
/*************************************************************
formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static string format(char value, string fd)
{
return formatinteger(value, fd, 16);
}
/*************************************************************
formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static string format(double value, string fd)
{
extractattributes(fd);
string s1;
if(base == 'b')
{
s1 = long.tobinarystring(double.doubletolongbits(value));
s1 = repeat(64, '0') + s1;
s1 = s1.substring(s1.length() - 64);
} else if(base == 'x')
{
s1 = long.tohexstring(double.doubletolongbits(value));
s1 = repeat(16, '0') + s1;
s1 = s1.substring(s1.length() - 16);
} else
{
pattern = decimals != -1 ? "." + repeat(decimals, '0') : ".#";
if(scientific)
pattern = "0" + pattern + "e0";
else
pattern = separator ? "#,##0" + pattern : "0" + pattern;
s1 = (new decimalformat(pattern)).format(value);
}
return size(s1);
}
/*************************************************************
formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static string format(float value, string fd)
{
extractattributes(fd);
string s1;
if(base == 'b')
{
s1 = integer.tobinarystring(float.floattointbits(value));
s1 = repeat(32, '0') + s1;
s1 = s1.substring(s1.length() - 32);
} else if(base == 'x')
{
s1 = integer.tohexstring(float.floattointbits(value));
s1 = repeat(8, '0') + s1;
s1 = s1.substring(s1.length() - 8);
} else
{
pattern = decimals != -1 ? "." + repeat(decimals, '0') : ".#";
if(scientific)
pattern = "0" + pattern + "e0";
else
pattern = separator ? "#,##0" + pattern : "0" + pattern;
s1 = (new decimalformat(pattern)).format(value);
}
return size(s1);
}
/*************************************************************
formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static string format(int value, string fd)
{
return formatinteger(value, fd, 32);
}
/*************************************************************
formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static string format(long value, string fd)
{
return formatinteger(value, fd, 64);
}
/*************************************************************
formats the passed value using the passed format descriptor
and returns the result as a string.
@param value the value to be formatted.
@param fd the format descriptor.
@return the formatted value as a string.
**************************************************************/
public static string format(short value, string fd)
{
return formatinteger(value, fd, 16);
}
/* formats the passed value using the passed format descriptor
* and returns the result as a string.
*/
private static string formatinteger(long l, string s, int i)
{
extractattributes(s);
string s1;
if(base == 'b')
{
s1 = long.tobinarystring(l);
s1 = repeat(64, '0') + s1;
s1 = s1.substring(s1.length() - i);
} else
if(base == 'x')
{
s1 = long.tohexstring(l);
s1 = repeat(16, '0') + s1;
s1 = s1.substring(s1.length() - i / 4);
} else
if(separator)
{
s1 = (new decimalformat("#,###")).format(l);
} else
{
s1 = string.valueof(l);
if(zerofill)
s1 = repeat(width - s1.length(), '0') + s1;
}
return size(s1);
}
// gets information from the passed format descriptor.
private static void extractattributes(string s)
{
s = s.touppercase();
alignment = 'r';
separator = false;
base = 'd';
scientific = false;
zerofill = false;
width = -1;
decimals = -1;
int i = s.indexof(76, 0);
if(i > -1)
{
alignment = 'l';
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexof(67, 0);
if(i > -1)
{
alignment = 'c';
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexof(44, 0);
if(i > -1)
{
separator = true;
pattern = pattern + ",###";
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexof(88, 0);
if(i > -1)
{
base = 'x';
s = s.substring(0, i) + s.substring(i + 1);
} else
{
i = s.indexof(66, 0);
if(i > -1)
{
base = 'b';
s = s.substring(0, i) + s.substring(i + 1);
}
}
i = s.indexof(83, 0);
if(i > -1)
{
scientific = true;
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexof(90, 0);
if(i > -1)
{
zerofill = true;
s = s.substring(0, i) + s.substring(i + 1);
}
i = s.indexof(46, 0);
if(i > -1)
{
decimals = integer.parseint(s.substring(i + 1));
s = s.substring(0, i);
}
if(s.length() > 0)
width = integer.parseint(s);
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptor. no trailing end-of-line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(byte value, string fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptor. no trailing end-of-line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(char value, string fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptor. no trailing end-of-line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(double value, string fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptor. no trailing end-of-line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(float value, string fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptor. no trailing end-of-line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(int value, string fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptor. no trailing end-of-line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(long value, string fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptor. no trailing end-of-line
character is printed.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void print(short value, string fd)
{
handle.print(format(value, fd));
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptorand followed by an end-of-line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(byte value, string fd)
{
print(value, fd);
handle.print(eol);
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptorand followed by an end-of-line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(char value, string fd)
{
print(value, fd);
handle.print(eol);
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptorand followed by an end-of-line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(double value, string fd)
{
print(value, fd);
handle.print(eol);
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptorand followed by an end-of-line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(float value, string fd)
{
print(value, fd);
handle.print(eol);
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptorand followed by an end-of-line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(int value, string fd)
{
print(value, fd);
handle.print(eol);
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptorand followed by an end-of-line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(long value, string fd)
{
print(value, fd);
handle.print(eol);
handle.flush();
}
/*************************************************************
output the passed value to the standard output device using
the passed format descriptorand followed by an end-of-line
marker.
@param value the value to be printed.
@param fd the format descriptor.
**************************************************************/
public static void println(short value, string fd)
{
print(value, fd);
handle.print(eol);
handle.flush();
}
// returns the string padding with 'fillchar'.
private static string size(string s)
{
int i = width - s.length();
if(alignment == 'r')
return repeat(i, fillchar) + s;
if(alignment == 'l')
return s + repeat(i, fillchar);
else
return repeat(i / 2, fillchar) + s + repeat(i / 2 + i % 2, fillchar);
}
// repeats the passed character 'times' times.
public static string repeat(int times, char c)
{
string s = "";
for(int i = 0; i < times; i++)
s = s + c;
return s;
}
private static final string eol = system.getproperty("line.separator");
private static printwriter handle = new printwriter(new outputstreamwriter(system.out));
public static char fillchar = ' ';
private static char alignment;
private static boolean separator;
private static char base;
private static boolean scientific;
private static boolean zerofill;
private static int width;
private static int decimals;
private static string pattern;
}
// class myouttester
// test the format of numbers in different ways.
public class myouttester
{
public static void main(string[] args)
{
int i = 230;
double d = 114.495678905;
// unformatted
system.out.print(i + " ");
system.out.println(d);
// some formatting
myout.print(i, "11");
myout.println(d, "14.3");
// more formatting
myout.print(i, "11l");
myout.println(d, ",14.7");
// scientific
myout.print(i, ",11");
myout.println(d, ",14.6s");
// hexdecimal
myout.print(i, "x11");
myout.println(d, "x21");
// changed fillchar
myout.fillchar = '*';
myout.print(i, "11");
myout.println(d, "14.3");
// special case
myout.println(d, "");
}
}
闽公网安备 35060202000074号