新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java语言代码格式化 java 格式化

java格式化的输出?

这句Java语句的格式是左对齐输出20个字符的字符串,不足的字符用空格填充.

创新互联专业为企业提供彰武网站建设、彰武做网站、彰武网站设计、彰武网站制作等企业网站建设、网页设计与制作、彰武企业网站模板建站服务,十余年彰武做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

s表示字符串

-表示左对齐

20表示20个字符

输出hello后面带15个空格.

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中怎么格式化日期??

你可以用String类的format方法,例如: System.out.println(String.format("%ty年%tm月%td日",date));下面是一个完整的例子。

public class FormatDateTest

{

public static void main(String[] args)

{

Date date = new Date(System.currentTimeMillis());

System.out.println(String.format("%ty年%tm月%td日",date));

System.out.println(String.format("%tY年%tm月%td日",date));

System.out.println(String.format("%tY年%tm月%td日%tH时%tM分%tS秒",date));

}

}

%ty是格式化年,%tm是格式化年,%td是格式化天,%tH格式化发时,%tM格式化分,%tS格式化秒。另外%tY是把年格式化为四位形式,如1999,而不是99。%tI是把时格式化为12小时制。格式化字符串中的是表示格式化同一个日期,当然你也可以这么写: System.out.println(String.format("%ty年%tm月%td日",date,date,date));

java 格式化输出

给你个最详细的, 确实有

package test;

import java.text.DecimalFormat;

public class 格式化 {

/**

* @param args

*/

public static void main(String[] args) {

DecimalFormat dig=new DecimalFormat("0000");

String s=dig.format(2);

System.out.print(s);

}

}

用Java编写数字格式化程序

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

public class FormateBankAccountId {

ListString standardBankAccountIdFormat ;

public FormateBankAccountId(String propertiesFileName) throws FileNotFoundException, IOException

{

// 加载资源文件

Properties properties = new Properties();

properties.load(new FileReader(propertiesFileName));

this.standardBankAccountIdFormat = new ArrayListString();

int keyNumber = 1;

String value = null;

// 读取键值对,键的格式是: formate_1 ,..., formate_10,...,formate_100 等

while( ( value = properties.getProperty("formate_" + keyNumber++) ) != null)

{

value = value.trim();

this.standardBankAccountIdFormat.add(value);

}

}

public ListString formate(String orginalBankAccountId)

{

ListString proceededlBankAccountIds = new :

java对数字格式化的几种方法

在NumberFormat类中为我们提供了格式化4种数字的方法:整数、小数、货币和百分比,通过工厂方法getNumberInstance, getNumberIntance, getCurrencyInstance, getPercentInstance方法获得相应的实例对象就行。例如我们要以字符串表示人民币88888.88元,这样来写就行:

NumberFormat nf = NumberFormat.getCurrencyInstance();

System.out.println(nf.format(88888.88));

定制格式化数字

可是对于稍微复杂一点的需求,NumberFormat就满足不了了,幸好java还提供了DecimalFormat实现定制的格式化。要使用DecimalFormat对象,必须提供给它提供一个格式化的模式(pattern):

String pattern = …

DecimalFormat df = new DecimalFormat(pattern);

或者:

DecimalFormat df = new DecimalFormat();

df. applyPattern(pattern);

然后就调用它的format方法就行了。


当前题目:java语言代码格式化 java 格式化
标题网址:http://www.wjwzjz.com/article/dodpseg.html
在线咨询
服务热线
服务热线:028-86922220
TOP