本文目錄一覽:
在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格式化數字是什麼意思?
在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
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裡面如何格式化數字
樓主你好!給你寫了個測試類希望能幫助你。這兩個個方法只需要傳入你要格式話的數據,就可以返回你想要的結果了。 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”));
}}
原創文章,作者:TVVC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/142310.html