- 浏览: 1591955 次
- 性别:
- 来自: 吉林
文章分类
- 全部博客 (624)
- C/C++ (33)
- Java (181)
- 网络相关 (7)
- 我爱篮球 (3)
- 也爱足球 (4)
- 杂谈 (1)
- 系统架构 (3)
- Web Service (14)
- Framework (3)
- 系统命令 (8)
- 管理平台相关 (8)
- 其它 (35)
- Websphere (1)
- Struts2 (24)
- Hibernate (16)
- Spring (23)
- javascript (20)
- jquery (23)
- html/css/div (28)
- 数据库 (40)
- JavaWeb (27)
- 设计模式 (2)
- 文档编写 (3)
- SVN (5)
- Ant (1)
- Maven (13)
- 软件项目管理 (8)
- AOP (1)
- kindeditor (1)
- JSON (2)
- Servlt/JSP (4)
- WordXML (2)
- XML (12)
- 面试相关 (7)
- Tomcat (11)
- 性能与调优 (29)
- 职业发展 (2)
- 操作系统 (7)
- AJAX (2)
- DWR (1)
- Eclipse (12)
- 持续集成 (3)
- 批处理命令 (1)
- Mozilla Rhino (2)
- 新鲜技术 (18)
- Apache mina (2)
- 底层技术 (18)
- Linux (22)
- 新鲜技术,IT历史 (1)
- 敏捷开发 (1)
- 版本控制 (5)
- 较火技术 (7)
- 集群 (2)
- Web前端 (13)
- 报表工具 (3)
- 网站架构 (5)
- 大数据 (8)
- 分布式存储 (5)
- 云计算 (8)
- TCP/IP协议 (1)
- 负载均衡 (3)
- 硬件 (1)
- 表现层技术 (3)
- Velocity (3)
- jvm (6)
- 并发编程 (10)
- hadoop (8)
- 数据结构和算法 (12)
- 计算机原理 (1)
- 测试驱动开发-TDD (3)
- 开发技巧 (1)
- 分词器 (1)
- 项目构建工具 (2)
- JMX (4)
- RMI (1)
- 测试技术 (22)
- 网络完全 (1)
- Git (4)
- apache开源包 (4)
- Java常用 (1)
- mock (2)
- OSGi (2)
- MongoDB (1)
- JBPM (1)
- Storm (3)
- mysql (2)
- telnet (1)
- 正则表达式 (1)
- bootstrap (4)
- Apache ActiveMQ (1)
- redis (9)
- Nginx (2)
- rsync+inotify文件同步 (2)
- testng (1)
- 原型设计工具 (1)
- 工程能力 (1)
- 风险控制 (3)
- ibatis (1)
- 分布式 (4)
- 安全技术 (1)
- 计算机基础 (4)
- 消息中间件 (1)
- UML (2)
最新评论
-
u012236967:
java命令执行jar包(里面的main函数)的方式(包括依赖其它的jar包问题) -
世界尽头没有你:
Selenium自动化测试从入门到精通(Java版)百度网盘地 ...
自动化测试工具 Selenium WebDriver 入门教程(针对主流浏览器) -
小小西芹菜:
我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下go ...
dwr实现Reverse Ajax推送技术的三种方式 -
hellozhouqiao:
楼主,请教一点问题.现在我们需要在excel 的页脚里面加上图 ...
FreeMaker + xml 导出word(处理目录,图片和页眉页脚问题) -
乱在长安:
使用Timer会有各种各样的问题好嘛?!书上推荐使用Sched ...
DelayQueue (ScheduledThreadPoolExecutor调度的实现)
转载自:http://www.chinaunix.net/jh/26/134984.html
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, ""); } }
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 MyOutimport 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, ""); } }
发表评论
-
Java SE7新特性之二进制字面量
2016-12-14 22:53 831Java SE 7中, 整数类型(byte, shor ... -
浮点数为什么不精确?为什么银行的金额不能用浮点数计算
2016-12-14 22:52 2028浮点数为什么不精确? 其实这句话本身就不精确, 相对精确一 ... -
计算机是如何计算乘法除法的(实际上都是转换为二进制加法计算)
2016-12-14 22:46 6634http://blog.csdn.net/blues1021 ... -
什么是补码、反码、负数在计算中如何表示
2016-12-14 22:39 924负数在计算机中如何表 ... -
Math.abs()绝对值取到的数不一定是正数
2016-12-14 22:37 1271Math.abs() 注释:Note that if th ... -
用BigInteger实现大素数生成算法
2016-12-06 16:42 1875转自:http://www.cnblogs.co ... -
加密(AES)、公私钥(PKI)、摘要(DIGEST)、签名(SIGNATURE)、证书(CERT)的形象解释和组合用法
2016-09-15 18:19 94231.鲍勃有两把钥匙,一把是公钥,另一把是私钥。 ... -
RSA加密公私钥生成工具(openssl)
2016-09-15 13:00 1197https://www.openssl.org/source/ ... -
RPC原理详解
2016-06-23 20:47 582RPC 功能目标 RPC 的主要功能目标是让构建分布式计算 ... -
Java并发之CountDownLatch、CyclicBarrier和Semaphore
2016-06-23 20:36 1417这次说一下 JUC 中的同步器三个主要的成员:CountDo ... -
线程间协作的两种方式:wait、notify、notifyAll和Condition
2016-06-23 20:35 580在前面我们将了很多关于同步的问题,然而在现实中,需要线程之 ... -
The final local variable xxx cannot be assigned, since it is defined in an enclo
2016-04-24 17:42 1601本文就自己编程时候遇到的一个问题,简要描述一下,并提出解决 ... -
一个奇葩的excel中无法trim的“空格”的问题
2016-03-29 16:21 1944太奇怪了。打印出来的命名是空格,就是 trim() 不管用。 ... -
List深度复制
2016-02-17 09:34 2688昨天弄了一个类似List<Map<String, ... -
Eclipse导入 jar 源码乱码问题
2015-12-15 19:43 883http://blog.csdn.net/laiwenqian ... -
Java高并发编程的各种陷阱
2015-07-25 17:49 1009www.iteye.com/blogs/subjects/ja ... -
ASM介绍(java字节码操作利器)
2015-06-10 20:46 4373转自:http://www.ibm.com/de ... -
JDK 7 中的 Fork/Join 模式(并发处理)
2015-06-03 19:47 1015转自:http://www.ibm.com/dev ... -
Java常见内存溢出异常分析
2015-06-03 16:36 833转自:http://www.iteye.com/news/3 ... -
dom4j解析带有命名空间的xml
2015-04-09 21:23 5542Element rootEle = resultDoc.g ...
相关推荐
总结来说,Java格式化XML文件涉及的主要知识点有:XML解析器的使用,如`DocumentBuilderFactory`和`DocumentBuilder`;`TransformerFactory`和`Transformer`用于XML的转换,包括设置输出属性;以及`Source`和`Result...
对于独立使用,可以直接通过命令行运行jar包,比如使用`java -jar SQLinForm.jar <input_sql_file> <output_sql_file>`,其中`<input_sql_file>`是包含原始SQL的文件,`<output_sql_file>`则是格式化后SQL的输出文件...
java组件开发(14) 格式化输出
Java格式化输出.txt
在给定的标题“SQL格式化功能(JAVA代码)”中,我们聚焦于一个特定的Java实现,它从Hibernate库中抽取了用于SQL格式化的功能,并且不依赖任何第三方jar包。 `Hibernate`是一个流行的Java ORM(对象关系映射)框架...
Java 字符串日期数字格式化输出 Java 中的字符串日期数字格式化输出是一种常见的操作,特别是在数据输出和显示时。 Java 提供了多种方式来实现格式化输出,包括使用 NumberFormat、DecimalFormat 和 ...
在Java编程语言中,控制输出...总结来说,`NumberFormat`和`java.math`包的`BigInteger`、`BigDecimal`类是Java中处理格式化输出和高精度计算的关键工具。它们提供了丰富的功能,确保了在各种场景下的灵活性和精度。
在Java编程中,格式化输出数字是一项常见的任务,特别是在数据展示和报表生成中。`java.text.DecimalFormat` 类是Java提供的一个强大工具,用于精确控制数字的显示方式。以下是对标题和描述中所述知识点的详细解释:...
java常规类型的格式化.doc ) 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
char n='150cm'; System.out.format("身高:%s", n); 举个例子 只要在双引号填加自己想说的话就好了 逗号后用来输出变量 占位符 说明 %d 格式化输出整数 %f 格式化输出浮点数 ...%s 格式化输出字符串
"标签"进一步强调了这个工具的关键特性,包括"Java输出漂亮的SQL语句",这意味着它是一个Java环境下的解决方案,能够集成到Java项目中,通过Java代码来调用和实现SQL语句的格式化。另一个标签"SQL格式化"则明确了它...
格式化程序可以作用于整个文件、有限行 ( --lines)、特定偏移量 ( --offset)、传递到标准输出(默认)或就地更改 ( --replace)。 要重新格式化特定补丁中更改的行,请使用 google-java-format-diff.py. 注意: ...
将浮点型数用美元符号和货币符号输出,用格式化的方式。还包括浮点型数据相除时,格式化输出结果。
在Java编程语言中,`printf`方法是`PrintStream`类的一个重要成员,它提供了类似于C语言中的格式化输出功能。这个方法允许我们以灵活的方式控制输出内容的格式,包括精度、宽度、对齐方式等。`moneytzb`可能是指特定...
当前的系统时间的格式化输出,包括中国时间和美国时间,并且是按照不同的两种方式输出的.
Java日期格式化是Java开发中常见的一项任务,它涉及到日期和时间的处理,尤其是在数据输入输出、用户界面显示以及数据库交互等场景。Java提供了一系列的类来帮助我们完成日期和时间的格式化工作,其中最常用的包括`...
Java格式化输出 Java中格式化输出是指使用格式化字符串和参数列表来输出想要的结果。Java中提供了多种方式来实现格式化输出,如System.out.printf()方法、String.format()方法、java.util.Formatter类等。 格式化...
Java编程语言中,字符串、日期和数字的格式化输出是一个重要的概念,特别是在处理用户界面输出或者数据记录时。虽然在C语言中可以使用`printf`函数进行格式化输出,但Java的标准输出`System.out.println()`并没有...
3. 如果一切顺利, Jacobe 会按照Sun标准格式重新格式化`your_file.java`,并覆盖原文件,或者你可以选择输出到新的文件。 在实际开发中,为了保证代码风格的一致性,许多开发团队会在版本控制系统(如Git)的pre-...
格式化人民币,能将您输入的数字格式化成人民币类型,欢迎下载。