這句Java語句的格式是左對齊輸出20個(gè)字符的字符串,不足的字符用空格填充.

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供彰武網(wǎng)站建設(shè)、彰武做網(wǎng)站、彰武網(wǎng)站設(shè)計(jì)、彰武網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、彰武企業(yè)網(wǎng)站模板建站服務(wù),十余年彰武做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
s表示字符串
-表示左對齊
20表示20個(gè)字符
輸出hello后面帶15個(gè)空格.
樓主你好!給你寫了個(gè)測試類希望能幫助你。這兩個(gè)個(gè)方法只需要傳入你要格式話的數(shù)據(jù),就可以返回你想要的結(jié)果了。 package com.line;public class T9 {
/**
* b格式化一般數(shù)據(jù)為財(cái)務(wù)格式,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--) {// 除去尾數(shù)0,小數(shù)點(diǎn)后保留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格式化財(cái)務(wù)數(shù)據(jù)為一般字符串,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"));
}}
你可以用String類的format方法,例如: System.out.println(String.format("%ty年%tm月%td日",date));下面是一個(gè)完整的例子。
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時(shí)%tM分%tS秒",date));
}
}
%ty是格式化年,%tm是格式化年,%td是格式化天,%tH格式化發(fā)時(shí),%tM格式化分,%tS格式化秒。另外%tY是把年格式化為四位形式,如1999,而不是99。%tI是把時(shí)格式化為12小時(shí)制。格式化字符串中的是表示格式化同一個(gè)日期,當(dāng)然你也可以這么寫:?System.out.println(String.format("%ty年%tm月%td日",date,date,date));
給你個(gè)最詳細(xì)的, 確實(shí)有
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);
}
}
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 :
在NumberFormat類中為我們提供了格式化4種數(shù)字的方法:整數(shù)、小數(shù)、貨幣和百分比,通過工廠方法getNumberInstance, getNumberIntance, getCurrencyInstance, getPercentInstance方法獲得相應(yīng)的實(shí)例對象就行。例如我們要以字符串表示人民幣88888.88元,這樣來寫就行:
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(88888.88));
定制格式化數(shù)字
可是對于稍微復(fù)雜一點(diǎn)的需求,NumberFormat就滿足不了了,幸好java還提供了DecimalFormat實(shí)現(xiàn)定制的格式化。要使用DecimalFormat對象,必須提供給它提供一個(gè)格式化的模式(pattern):
String pattern = …
DecimalFormat df = new DecimalFormat(pattern);
或者:
DecimalFormat df = new DecimalFormat();
df. applyPattern(pattern);
然后就調(diào)用它的format方法就行了。
分享題目:java語言代碼格式化 java 格式化
路徑分享:http://chinadenli.net/article46/dodpseg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站設(shè)計(jì)、定制開發(fā)、企業(yè)網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容