2016-11-25 60 views
0

簡單我想使我從一個字符串輸出(TextView的)像Rp.40000是Rp.40.000將文本設置爲十進制格式

這裏是我的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_detail); 
    //Mengambil data dari intent 
    DecimalFormat formatter = new DecimalFormat("#,###,###"); 
    Intent i=getIntent(); 
    String npwp = i.getStringExtra(TAG_NPWP); 
    String statusspp = i.getStringExtra(TAG_STATUSSPP); 
    String tglsp2d= i.getStringExtra(TAG_TGLSP2D); 
    String jumlahtotal = i.getStringExtra(TAG_JUMLAH); 
    TextView textName=(TextView)findViewById(R.id.textName); 
    TextView textLat=(TextView)findViewById(R.id.textLat); 
    TextView textLon=(TextView)findViewById(R.id.textLon); 
    TextView textPrice=(TextView)findViewById(R.id.textPrice); 
    textName.setText("Nama : "+npwp); 
    textLat.setText(statusspp); 
    textLon.setText("TglSp2D : "+tglsp2d); 
    textPrice.setText("Jumlah : Rp "+jumlahtotal); 
+0

你的問題是什麼。不明白?編輯你的佇列。 –

+0

@RizkiDeddySusanto,似乎你從來沒有使用你的格式化程序。 – lmiguelvargasf

+0

'textPrice.setText(「Jumlah:Rp」+ formatter.format(jumlahtotal));' –

回答

1

我希望這有助於:

NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(new Locale("in", "ID")); 
textPrice.setText("Jumlah : "+ numberFormatter.format(40000)); 

而不是40000傳給你想格式的數字。

編輯:如果你有值是一個字符串,它是一個有效的數字:

NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(new Locale("in", "ID")); 
Double number = Double.parseDouble(yourString); 
textPrice.setText("Jumlah : "+ numberFormatter.format(number)); 
+0

值怎麼樣是從字符串JSON(jumlahtotal是價值) –

+0

@RizkiDeddySusanto,我編輯了我的答案,所以我希望這有助於。 – lmiguelvargasf

+0

偉大的先生,其工作..感謝您的幫助 –

0

您可以使用

DecimalFormatSymbols.setGroupingSeperator

public static void main(String[] args) { 
    int val = 40000; 
    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(); 
    dfs.setGroupingSeparator('.'); 
    DecimalFormat formatter = new DecimalFormat("#,###",dfs); 
    System.out.println(formatter.format(val)); 
}