import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.DecimalFormat;
import java.math.BigDecimal;
import java.text.DateFormat;
/**
* @author DingDang
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Formater {
static DecimalFormat dfAsInt = new DecimalFormat("0");
static DecimalFormat dfAsKg = new DecimalFormat("0.00");
static DecimalFormat dfPoint1 = new DecimalFormat("0.0");
static DecimalFormat dfPoint2 = new DecimalFormat("0.00");
static DecimalFormat dfPoint4 = new DecimalFormat("0.0000");
static DecimalFormat dfPoint3 = new DecimalFormat("0.000");
static DecimalFormat dfPoint4_q = new DecimalFormat(",##0.0000");
static DecimalFormat dfPoint3_q = new DecimalFormat(",##0.000");
static DecimalFormat dfPoint2_q = new DecimalFormat(",##0.00");
static SimpleDateFormat mmmddyyyy = new SimpleDateFormat("MMM dd,yyyy", Locale.ENGLISH);
static SimpleDateFormat yyyyZmmZddZ = new SimpleDateFormat("yyyy年MM月dd日");
static SimpleDateFormat yyyy_mm_dd = new SimpleDateFormat("yyyy-MM-dd");
static SimpleDateFormat yyyy_mm_ddHHmmSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static SimpleDateFormat hhMMss = new SimpleDateFormat("HH:mm:ss");
public static String formatAsDateTime(Date date) {
return yyyy_mm_ddHHmmSS.format(date);//yyyy-MM-dd HH:mm:ss
}
public static Date parseAsDateTime(String s) throws ParseException {
return yyyy_mm_ddHHmmSS.parse(s);//yyyy-MM-dd HH:mm:ss
}
public static Date parseAsDate(String s) throws ParseException {
return yyyy_mm_dd.parse(s);//yyyy-MM-dd
}
public static Date parseAsCnDate(String s) throws ParseException {
return yyyyZmmZddZ.parse(s);//yyyy年MM月dd日
}
public static String formatAsCnDate(Date date) {
return yyyyZmmZddZ.format(date);//yyyy年MM月dd日
}
public static String formatAsEnDate(Date date) {
if(date==null) return "01 01,1971";
return mmmddyyyy.format(date);//MMM dd,yyyy
}
/**
* 短日期,不带时间
* @param date
* @return
*/
public static String formatAsDate(Date date) {
if(date==null) return "1971-01-01";
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return yyyy_mm_dd.format(date);
}
/**
* 短日期
* @param date
* @return
*/
public static String formatAsDateyy_MM_dd(Date date) {
if(date==null) return "71-01-01";
//SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
return yyyy_mm_dd.format(date);
}
public static String formatAsTime(Date date) {
return hhMMss.format(date);//HH:mm:ss
}
//",##0.000"
public static String formatCurrency(double dbl) {
return dfPoint3_q.format(dbl);
}
public static String formatCurrency2(double dbl) {
return dfPoint2_q.format(dbl);
}
public static String formatCurrency3(double dbl) {
return dfPoint3_q.format(dbl);
}
public static String formatCurrency3(BigDecimal dbl) {
return dfPoint3_q.format(dbl);
}
public static String formatCurrency4(double dbl) {
return dfPoint4_q.format(dbl);
}
public static String formatCurrency4(BigDecimal bd) {
return dfPoint4_q.format(bd);
}
public static String formatCurrency(BigDecimal bd) {
return dfPoint3.format(bd);
}
public static String formatCurrency4L(BigDecimal bd) {//四位小数
return dfPoint4.format(bd);
}
public static String formatCurrency4L(double bd) {//四位小数
return dfPoint4.format(bd);
}
public static String formatCurrency2(BigDecimal bd) {
return dfPoint2.format(bd);
}
public static String formatCurrency1(BigDecimal bd) {
return dfPoint1.format(bd);
}
public static String formatAsKg(BigDecimal bd) {
return dfAsKg.format(bd);
}
public static String formatAsKg(double dbl) {
return dfAsKg.format(dbl);
}
public static String formatAsInt(BigDecimal bd) {
return dfAsInt.format(bd);
}
public static String formatAsInt(double dbl) {
return dfAsInt.format(dbl);
}
}
分享到:
相关推荐
主要介绍了java 使用DecimalFormat进行数字的格式化实例详解的相关资料,需要的朋友可以参考下
在Java中使用java.text.DecimalFormat格式化数字为指定形式后输出,以满足实际需要;DecimalFormat类是NumberFormat类的一个子类,用于格式化十进制数字,可以将数字格式化为整数、浮点数、百分比数等,并且可以为要...
主要为大家详细介绍了DecimalFormat数字格式化用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了java.text.DecimalFormat类十进制格式化的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了Java中DecimalFormat的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了java DecimalFormat的常用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
decimalformat是Java的类的Node实现。 安装 npm install @deloitte-digital-au/decimalformat 用法 使用DecimalFormat类 const { DecimalFormat } = require ( 'DecimalFormat' ) ; const df = new DecimalFormat ...
在Java程序设计过程中,对应日期和时间的格式化,还有一个简单的格式化方式,就是java.text.SimpleDateFormat,该类中用字符串指定日期和时间的格式,字符串中的字符称为模式字符,模式字符区分大小写。常见的模式...
1、格式化小数 123456.123 -> 123,456.12 2、格式化整数 123456 -> 123,456
格式化字符串(数字,时间)。 DecimalFormat format=new DecimalFormat("#,####.##"); //创建格式化字符串的对象
主要为大家详细介绍了java.text.DecimalFormat用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
2. 在静态代码块中,初始化了一个DecimalFormat 对象 df用于格式化文件大小的显示。 3. server类的构造函数通过调用 super(SERVER_PORT)来创建一个服务器端的Socket。 4. load()方法是一个循环,用于接收客户端的...
这个源码资源是一个讲解数学计算处理类的示例项目,旨在帮助开发者理解和使用Java中的数学计算相关类库。该项目包含详细的代码示例和注释,涵盖了以下...理解DecimalFormat类的功能,包括数字格式化、舍入规则设置等。
主要为大家详细介绍了java中DecimalFormat四舍五入的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例讲述了Android实现的数字格式化用法。分享给大家供大家参考,具体如下: package formatnumber; import java.text.DecimalFormat; public class FormatNumber { public static void main(String[] args) { ...
的DecimalFormat用于格式化从用户输入导出的数字。 要求 安卓 4.1 (API 16) 安装 将 Jitpack 添加到您的项目 build.gralde 文件中 allprojects { repositories { maven { url 'https://jitpack.io' } } } 然后将此...
DecimalFormat精度解决,商业运算精度问题,BigDecimal类的应用--------------
用java实现的计算器 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("#.00"); float sumWT=0; float sumWWT=0; float AverageWT; for(int i=0;i;i++){ System.out.println("时刻"+p[i].startTime+": 进程"+p[i].pid+"开始运行,完成...
import java.text.DecimalFormat; public class Calculation { public static final DecimalFormat df = new DecimalFormat("######0.00"); /** * @methodName format2point * @desc 保留两位小数点 * @param...