将文本从字符串设置为十进制格式

时间:2016-11-25 03:57:14

标签: java android string settext

简单我想让我的输出(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);

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分: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));
}
相关问题