# Doubt with decimals Android (BigDecimal)

3

When I make an account to get the % of a number, I have a problem, I explain:

I take 5% out of 7.74 and it's 0.3870 but I want you to only show me two numbers in the decimal part, that is < strong> 0.38 How can I do this?

And another doubt that I have, is it possible that the result is with a , and not a . ?

This is how I do the process:

``````public class MainActivity extends AppCompatActivity {

EditText uno;
TextView tres;
Button btn1;

static final BigDecimal PORCENTAJE_CINCO = new BigDecimal("0.05");

public BigDecimal calculaCincoPorCiento(BigDecimal numero) {
return numero.multiply(PORCENTAJE_CINCO);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

uno = (EditText) findViewById(R.id.uno);
tres = (TextView) findViewById(R.id.tres);

btn1 = (Button) findViewById(R.id.btn1);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BigDecimal aux0 = new BigDecimal(uno.getText().toString());
BigDecimal aux1 = calculaCincoPorCiento(aux0);
tres.setText(aux1 + " " + "es el 5%");
}
});
}
}
``````

Thank you!

source

3

You can use `setScale` to show the number of decimals you want, and also, you can put it if you want to round it halfway up or down. Here is an example of rounding up with two decimals:

``````BigDecimal bd = new BigDecimal(3.33333);
bd = bd.setScale(2, RoundingMode.HALF_UP);
``````

To show it with a `,` instead of a `.` you can convert it to `String` and then substitute the `.` for a `,` :

``````TextView tv = (TextView)findViewById(R.id.texto);
BigDecimal bd = new BigDecimal(3.33333);
tv.setText(bd.toString().replace(",","."));
``````