Tuesday, May 10, 2011

Program to format the decimal display in Java

I need a generalized format (00.00) to display the parameter ‘Speed’ in Java.
For eg:- If the speed is 10.5, I need it to be displayed in the format ‘10.50’.

The DecimalFormat Class can be used to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.

import java.text.DecimalFormat;

public class doubleFormat
{
 public static void main(String[] args)
 {
  DecimalFormat dfSpeedFormatter = new DecimalFormat("00.00");
  String strSpeed;
  double dSpeed = 0.5;
  strSpeed = dfSpeedFormatter.format(dSpeed);
  System.out.println(strSpeed);
 }
}

The output will be displayed as:
00.50



Source:- http://download.oracle.com/javase/tutorial/java/data/numberformat.html

Monday, May 2, 2011

Change file ownership in Linux

How to Change the Ownership of a File in Linux

I listed the file using the command ls -l in terminal.
The command displayed the ownership of the file as my.
-rw-r--r-- 1 my my 420 2011-05-02 11:03 IP.dat


I need to convert the ownership of IP.dat file to root

Type the command,
chown root:root IP.dat
in terminal.

The command has changed the ownership of the file to root
-rw-r--r-- 1 root root 420 2011-05-02 11:03 IP.dat