Untuk membuat format number di java dapat digunakan DecimalFormat atau NumberFormat।Supaya tidak lupa aq tulis di blog saja ah.
esnips.com/doc/8614e892-0a92-437b-8e3b-574fad318fce/MyNumberFormat
====================================================================
package test1;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class MyNumberFormart {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Double nilai = new Double(1234.6);
DecimalFormat gaPakaiTanda = new DecimalFormat("###,##0.00");
System.out.println("\nNilai (dengan format DecimalFormat(''###,##0.00'')) = " + gaPakaiTanda.format(nilai));
DecimalFormat pakaiTanda = new DecimalFormat("$###,##0.00");
System.out.println("\nNilai (dengan format DecimalFormat(''$###,##0.00'') =" + pakaiTanda.format(nilai));
NumberFormat instanceFormat = NumberFormat.getCurrencyInstance();
System.out.println("\nNilai (dengan NumberFormat.getCurrencyInstance() ) = " + instanceFormat.format(nilai));
NumberFormat dgFormat = NumberFormat.getInstance();
System.out.println("\nNilai (dengan NumberFormat.getInstance() ) = " +dgFormat.format(nilai));
Double dataPersen = new Double(0.35);
NumberFormat formatPersen;
String data = new String("");
formatPersen = NumberFormat.getPercentInstance();
data = formatPersen.format(dataPersen);
System.out.println("\nNilai Data dalam Format Persen = "+data);
}
}
====================================================================
ओउत्पुत:
Nilai (dengan format DecimalFormat(''###,##0.00'')) = 1,234.60
Nilai (dengan format DecimalFormat(''$###,##0.00'') =$1,234.60
Nilai (dengan NumberFormat.getCurrencyInstance() ) = $1,234.60
Nilai (dengan NumberFormat.getInstance() ) = 1,234.6
Nilai Data dalam Format Persen = 35%
1 comment:
asli makasih banget gan.
liat di blog sebelah rumitnya minta ampun, sedangkan disini dpt pencerahan yang simple tpi berguna banget
thanks... !
Post a Comment