本文目錄一覽:
- 1、JAVA中怎樣使用format進行對常規類型格式化的方法
- 2、JAVA裡面如何格式化數字
- 3、java怎麼輸出貨幣符號
- 4、Java 將數字格式化為貨幣字符竄。。。下面這個程序應該怎麼改!求高手!!
- 5、java.text.DecimalFormat如何將數字格式化為金額格式,而且小數點保留任意位
- 6、java對數字格式化的幾種方法
JAVA中怎樣使用format進行對常規類型格式化的方法
你要格式化什麼?日期?數字?下面給你舉兩個例子吧。日期的格式化可以使用java.text.SimpleDateFormat 類,例如:java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(“現在是:yyyy年MM月dd日 hh時mm分”);String now=sdf.format(new java.util.Date();System.out.println(now);同樣,我們也可以把它反過來,即把字符串格式化為java.util.Date對象:java.util.Date dd = sdf.parseDate(“現在是:2010年02月02日 08時56分”);
下面是把數字形式的金額格式化為貨幣的形式:java.text.DecimalFormat df=new java.text.DecimalFormat(“,##0.00”);System.out.println(“總金額為:” + df.format(123456.7890d));
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怎麼輸出貨幣符號
java輸出貨比符號測試方法:
import java.text.NumberFormat;
import java.util.Locale;
public class FormatTest {
public static void main(String args[]) {
// 不使用格式化輸出數
double d = 10.0 / 3.0;
System.out.println(“無格式化輸出:” + d);
// 使用本地默認格式輸出數
NumberFormat numberFormat = NumberFormat.getNumberInstance();
//numberFormat.setMaximumFractionDigits(4);
//numberFormat.setMinimumIntegerDigits(6);
String numberString = numberFormat.format(d);
System.out.println(“本地默認格式輸出數:” + numberString);
// 使用本地默認格式輸出貨幣值
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
System.out.println(“本地默認格式輸出貨幣值:” + currencyFormat.format(d));
// 使用本地默認格式輸出百分數
NumberFormat percentFormat = NumberFormat.getPercentInstance();
System.out.println(“本地默認格式輸出百分數:” + percentFormat.format(d));
// 在不同的國家和地區數字表示的格式也有區別。如德國
// 使用德國的格式化輸出數
NumberFormat numberFormatG = NumberFormat
.getNumberInstance(Locale.GERMANY);
System.out.println(“德國數字輸出形式:” + numberFormatG.format(d));
// 使用德國貨幣輸出形式
NumberFormat currencyFormatG = NumberFormat
.getCurrencyInstance(Locale.GERMANY);
System.out.println(“德國貨幣輸出形式:” + currencyFormatG.format(d));
// 使用美國貨幣輸出形式
NumberFormat currencyFormatA = NumberFormat
.getCurrencyInstance(Locale.US);
System.out.println(“美國貨幣輸出形式:” + currencyFormatA.format(d));
// 使用德國百分數輸出形式
NumberFormat percentFormatG = NumberFormat
.getPercentInstance(Locale.GERMANY);
System.out.println(“德國百分數輸出形式:” + percentFormatG.format(d));
System.exit(0);
}
}
Java 將數字格式化為貨幣字符竄。。。下面這個程序應該怎麼改!求高手!!
這裡不對:Number number=new Number(System.in);
Number是一個抽象類,不能被實例化的 。
還有,就沒有NumberFormat.FULL這個屬性
java.text.DecimalFormat如何將數字格式化為金額格式,而且小數點保留任意位
DecimalFormat myformat = new DecimalFormat();
myformat.applyPattern(“##,###.000”);
System.out.println(myformat.format(11112345.12345));
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方法就行了。
原創文章,作者:JWPTH,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/315959.html