What’s the best way to present only two houses in a java float

Asked

Viewed 1,848 times

0

I am creating a simple application in android studio, where I make a division and present its result in an Edittext, however I would like to present only two decimal places, which is the best way to do this?

  • 7

    You can use it like this: String.format("%.2f", valor);

  • 2

    @cat I think you should post this as an answer :)

1 answer

1


Use this code to format your decimals in float or double

float x = (float) 12.309989;
    DecimalFormat df = new DecimalFormat("0.00");
    editText.setText(df.format(x));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.