2016-11-25 59 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)); 
}