[Java](NumberFormat) DecimalFormat

来源:互联网 发布:软件功能结构图 visio 编辑:程序博客网 时间:2024/05/21 06:43
import java.text.DecimalFormat;public class test {public static void main(String [] args ){double pi=3.1415927;DecimalFormat df=new DecimalFormat("0");<span style="white-space:pre"></span>//取一位整数System.out.println(df.format(pi));//Output: 3df=new DecimalFormat("0.00");<span style="white-space:pre"></span>//取一位整数和两位小数,不足补零System.out.println(df.format(pi));//Output: 3.14df=new DecimalFormat("00.000");<span style="white-space:pre"></span>//取两位整数和两位小数,不足补零System.out.println(df.format(pi));//Output: 03.14df=new DecimalFormat("#");<span style="white-space:pre"></span>//取整System.out.println(df.format(pi));//Output: 3df=new DecimalFormat("#.##%");<span style="white-space:pre"></span>//取两位小数,百分比形式System.out.println(df.format(pi));//Output: 3.1416%long c=299792458;<span style="white-space:pre"></span>//光速df=new DecimalFormat("#.#####E0");//取五位小数,科学计数法System.out.println(df.format(c));//Output: 2.99792E8df=new DecimalFormat("00.####E0");<span style="white-space:pre"></span>//取两位整数四位小数,科学计数法System.out.println(df.format(c));//Output: 29.9792E7df=new DecimalFormat(",###");<span style="white-space:pre"></span>//每三位以逗号分开System.out.println(df.format(c));//Output: 299,792,458df=new DecimalFormat("Light Speed is ,### m/s");<span style="white-space:pre"></span>//插入文本System.out.println(df.format(c));//Output: Light Speed is 299,792,458 m/s}}

0 0
原创粉丝点击