您的位置:

java格式化,JAVA格式化

本文目录一览:

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 (dotIndex 3) {

index += 1;

str.append(source.substring(0, dotIndex));

}

if (dotIndex % 3 == 0) {

index += dotIndex / 3;

} else {

index += (dotIndex - dotIndex % 3) / 3;

}

if (index 0 dotIndex = 3) {

for (int i = index; i 0; i--) {

if (i == index) {

str.append(source.substring(0, dotIndex - i * 3));

}

if (dotIndex - i * 3 0) {

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();

char[] strArr = str.toString().toCharArray();

StringBuffer rev = new StringBuffer();

for (int i = str_len - 1; i 0; i--) {// 除去尾数0,小数点后保留2位

if (i dot_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 (lastIndex 0) {

for (int i = 0; i myarr.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) {

T9 t=new T9();

System.out.println(t.getCaiWuData("1231313"));

System.out.println(t.formatStr("1,231,313.00"));

}}

在java里,什么是格式化字符串

格式化字符串就是按一定格式输出的字符串

举个例子来说,你想输出时间

2006-07-25

21:20:30

这就是一个格式化字符串,它的格式为

yyyy-mm-dd

hh:mm:ss

下面再给你一个操作的例子,比如说你要输出时间

formatdatetime('yyyy-mm-dd

hh:mm:ss',now);

就能够按我上面的格式输出时间了

又比如说你经过计算得到一个结果,你想用一个有格式的字符串输出

例如:计算结果是xxx,那么可以这样写:

format('计算结果是%d',[result]);

我给出的是delphi代码,如果你用c的,用printf语句也可以实现

printf("计算结果是%d",result);

JAVA 中获取时间怎么格式化?

时间格式化输出主要有两种方式,代码如下:

//使用Calendar

Calendar now = Calendar.getInstance();

System.out.println("年:" + now.get(Calendar.YEAR));

System.out.println("月:" + (now.get(Calendar.MONTH) + 1));

System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH));

System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY));

System.out.println("分:" + now.get(Calendar.MINUTE));

ystem.out.println("秒:" + now.get(Calendar.SECOND));

//使用Date

Date d = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println("当前时间:" + sdf.format(d));

扩展资料

JAVA中获取当前系统时间。

import java.util.Date;

import java.text.SimpleDateFormat;

public class NowString {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

}

}

参考资料来源:百度百科:Java

java怎么格式化输出数字

使用System.out.printf(格式化字符串,参数)

int a = 5;

数字的话System.out.printf("%d",a);

//"%"表示进行格式化输出,"%"之后的内容为格式的定义。

System.out.printf("%f",d);//"f"表示格式化输出浮点数。

System.out.println();

System.out.printf("%9.2f",d);//"9.2"中的9表示输出的长度,2表示小数点后的位数。

System.out.println();

System.out.printf("%+9.2f",d);//"+"表示输出的数带正负号。

System.out.println();

System.out.printf("%-9.4f",d);//"-"表示输出的数左对齐(默认为右对齐)。

System.out.println();

System.out.printf("%+-9.3f",d);//"+-"表示输出的数带正负号且左对齐。

System.out.println();

System.out.printf("%d",i);//"d"表示输出十进制整数。

System.out.println();

System.out.printf("%o",i);//"o"表示输出八进制整数。

System.out.println();

System.out.printf("%x",i);//"d"表示输出十六进制整数。

System.out.println();

System.out.printf("%#x",i);//"d"表示输出带有十六进制标志的整数。

System.out.println();

System.out.printf("%s",s);//"d"表示输出字符串。

System.out.println();

System.out.printf("输出一个浮点数:%f,一个整数:%d,一个字符串:%s",d,i,s);

//可以输出多个变量,注意顺序。

System.out.println();

System.out.printf("字符串:%2$s,%1$d的十六进制数:%1$#x",i,s);

//"X$"表示第几个变量。

java数字如何格式化?

public static String xxx(String aa) {

String flag = "";

if (aa.length() 4) {

flag = aa.substring(0, 3) + "." + aa.substring(3, 4);

} else {

flag = aa;

}

return flag;

}

//当然可以继续判断大于5为四舍五入

延展阅读:

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

java格式化数字是什么意思?

在java中,用java.text包下的DecimalFormat类对数据进行格式化,它可以把数字格式化成一个你想要那种格式的字符串,也可以把格式字符串变成数字:例如:

DecimalFormat df=new DecimalFormat("¥###,###.00");

String s=df.format(123.45);

System.out.println(s); 结果为字符串¥123.45

当然也可以字符串转成数字:

System.out.println(df.parse(s)); 结果为数字123.45