how to set the number format for an EditText

15,538

Solution 1

You can use DecimalFormat

DecimalFormat format = new DecimalFormat("##.#");
String formattedText = format.format(025.0);
editText.setText(formattedText);

You will get result in EditText as 25.0

Solution 2

you can set it in xml file. Like

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="54dp"
    android:ems="10"
    android:inputType="numberDecimal" >
Share:
15,538
Newton
Author by

Newton

Just a casual developer

Updated on June 04, 2022

Comments

  • Newton
    Newton almost 2 years

    I want the number being displayed in my EditText upto 1 decimal place with no leading zeroes e.g. 25.0 not like 025.0

    how can i do this?

    this is in android