java格式化數字
1. java中如何從格式化字元串輸入數值
什麼意思??格式化字元串??
是說要按照一定的格式書寫么用String.matches("正則表達式")
輸入數值可以通過valueof(object
obj);轉換成字元串
一般有重寫的toString()方法也可以轉換成字元串!!
要是這個意思的話,請點贊!!!!
2. 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為四捨五入
3. 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$"表示第幾個變數。
4. java 數字格式化
publicstaticStringxxx(Stringaa){
Stringflag="";
if(aa.length()>4){
flag=aa.substring(0,3)+"."+aa.substring(3,4);
}else{
flag=aa;
}
returnflag;
}
//當然可以繼續判斷大於5為四捨五入
5. 用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 {
List<String> standardBankAccountIdFormat ;
public FormateBankAccountId(String propertiesFileName) throws FileNotFoundException, IOException
{
// 載入資源文件
Properties properties = new Properties();
properties.load(new FileReader(propertiesFileName));
this.standardBankAccountIdFormat = new ArrayList<String>();
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 List<String> formate(String orginalBankAccountId)
{
List<String> proceededlBankAccountIds = new :
6. java處理數字格式的幾種方式
下面給你介紹java處理數字格式的3種方式:
double val = 1234.56;
1、轉成貨幣
DecimalFormat.getCurrencyInstance().format(val)
結果:¥1,234.56
2、轉成百專分比
DecimalFormat.getPercentInstance().format(val)
結果:123,456%
3、轉成千分位屬DecimalFormat.getNumberInstance().format(val)
結果:1,234.56
7. java數字格式化
用 %3.2f格式化後, 替換逗號即可;
同理, 用 #,##0.00 格式化後, 替換, 為空格即可.
你的需求:
doublex=1234.5;
DecimalFormatdf=newDecimalFormat("#,###.0");
Stringxs=df.format(x);
xs=xs.replace(",","").replace(".",",");
System.out.println(xs);
輸出
1234,5
8. 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方法就行了。
9. JAVA裡面如何格式化數字
java提供了格式化double類型的方法:NumberFormat.getInstance().format(a);這個是java內置的函數可以直接格式化double類型的數字;NumberFormat要導入import java.text.NumberFormat;即可。
10. java請將數字1429339937748格式化為日期,格式:YYYY-MM-DD
publicstaticvoidmain(String[]args){
Datedate=newDate(1429339937748l);//以1429339937748為毫秒數實例化一個Date對象
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");//設置轉化格式
Stringtime=sdf.format(date);//將Date對象轉化為yyyy-MM-dd形式的字元串
System.out.println(time);//輸出字元串
}
輸出結果為:2015-04-18,希望能幫到你