Android format number

Formatting a number is a common task when your application works with numbers (e.g. Calculator). In this Android tip, I am going to show you how to format a number in Android. In Android, you can format a number using DecimalFormat class. You can apply format pattern to the number using its applyPattern() method and call its format() method to format the number based on the applied pattern. For example, to format a number to include thousand separator (,) and to have two digits the decimal point, the pattern is "#,###.00". Generally, a DecimalFormat instance is cast from NumberFormat instance.

Now to have a workable example application on formatting a number, you create a new Android project and then edit the activity_main.xml file to add two EditTexts, one Button, and one TextView. The two EditTexts are for the user to enter numbers. The button is clicked to add the number and display the result in the TextView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:orientation="vertical"
   >

   <EditText
     android:id="@+id/txt_val1"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:hint="Enter value1"
     android:inputType="numberDecimal"
     />
   <EditText
     android:id="@+id/txt_val2"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:hint="Enter value2"
     android:inputType="numberDecimal"  
      />
  <Button
     android:id="@+id/bt_sum"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Sum"
     android:onClick="sumValues"
     />
<TextView
     android:id="@+id/txt_result"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textSize="20sp"
    
      />
</LinearLayout>


Open the MainActivity.java file and add the sumValues method to the MainActivity class. The sumValues method read numbers from the two EditTexts, sums them, and shows the result in the TextView.

  import java.text.NumberFormat;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

Activity context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;


}

public void sumValues(View view){
//reference to the first EditText
EditText txtValue1=(EditText)findViewById(R.id.txt_val1);
//get value from the the first EditText
String value1=txtValue1.getText().toString();
//reference to the second EditText
EditText txtValue2=(EditText)findViewById(R.id.txt_val2);
//get value from the the first EditText
String value2=txtValue2.getText().toString();
//reference to the TextView
TextView tv=(TextView)findViewById(R.id.txt_result);
//sum value1 and value2
double result=Double.parseDouble(value1)+Double.parseDouble(value2);
//format result and display it in TextView
NumberFormat nf=NumberFormat.getInstance();
DecimalFormat df=(DecimalFormat)nf;
df.applyPattern("#,###.00");
tv.setText("Result:"+df.format(result));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Android-format-number

Posted by: Dara | post date: 08-13-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.