Did this blog help you solve a problem? Tip me with Bitcoins at: 17dQktcAmU4urXz7tGk2sbuiCqykm3WLs6

Tuesday, July 13, 2010

Format a Decimal Number According to Local Formatting Standards in Java

To format a decimal number (double) according to different number formatting standards in different countries we can use the java.text.DecimalFormat, java.text.NumberFormat, and java.util.Locale classes. The following code example demonstrates formatting a decimal number in various country-specific styles.


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

//Java 1.4+ Compatible
//
// The following example code demonstrates converting a number 
// (double) into a formatted String according to different 
// number formatting standards in various countries
//
public class FormatDecimalLocalFormat {

public static void main(String[] args) {

// circumference of earth in km
double number = 40075.168776;

// Germany
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN);
System.out.println(df.format(number));

// United states
df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
System.out.println(df.format(number));

// China
df = (DecimalFormat) NumberFormat.getInstance(Locale.CHINESE);
System.out.println(df.format(number));

// France
df = (DecimalFormat) NumberFormat.getInstance(Locale.FRENCH);
System.out.println(df.format(number));

}
}



Here is the output of the example code:
40.075,169
40,075.169
40,075.169
40 075,169

Format a Decimal Number Using Scientific Notation in Java

To format a decimal number (double) using scientific notation in Java we can use the java.text.DecimalFormat class. The following code example demonstrates formatting the number of decimal digits and the number exponent digits of a decimal number in scientific notation.


import java.text.DecimalFormat;

// Java 1.4+ Compatible
//
// The following example code demonstrates converting a number 
// (double) into a formatted String in scientific notation setting
// the number of digits after the decimal place and in the exponent
//
public class FormatDecimalScientificNotation {

public static void main(String[] args) {

// speed of light in m/s
double number = 299792458;

// scientific notation, three decimal places, one exponent digit
DecimalFormat df = new DecimalFormat("0.000E0");
System.out.println(df.format(number));

// scientific notation, lower case e, two decimal places, two exponent digits
df = new DecimalFormat("0.00E00");
System.out.println(df.format(number).toLowerCase());

}

}



Here is the output of the example code:
2.998E8
3.00e08

Format a Decimal Number (Double) in Java

To format a decimal number (double) in Java we can use the java.text.DecimalFormat class. The following code example demonstrates formatting the number of decimal digits and the thousands separator of a decimal number.


import java.text.DecimalFormat;

// Java 1.4+ Compatible
//
// The following example code demonstrates converting a number 
// (double) into a formatted String setting the number of digits 
// after the decimal place and the separator between the thousands groups
//
public class FormatDecimalNumber {

public static void main(String[] args) {

// circumference of earth in km
double number = 40075.1646;

// thousands separator, three decimal places
DecimalFormat df = new DecimalFormat("###,###.###");
System.out.println(df.format(number));

// no thousands separator, two decimal places
df = new DecimalFormat(".##");
System.out.println(df.format(number));

}
}



Here is the output of the example code:
40,075.165
40075.16

Saturday, July 10, 2010

Convert an Array into a List in Java

To convert an Array into a java.util.List we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to a List of Strings.

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
* Java 1.4+ Compatible 

* The following example code demonstrates converting an Array of Strings to a List of Strings
*/
public class Array2List {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to List
List l = Arrays.asList(sa);

// iterate over each element in List
Iterator iterator = l.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Convert an Array into a LinkedList in Java

To convert an Array into a java.util.LinkedList we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to a LinkedList of Strings.

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

/**
* Java 1.4+ Compatible
* The following example code demonstrates converting an Array of Strings to a LinkedList of Strings
*/
public class Array2LinkedList {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to LinkedList
LinkedList ll = new LinkedList(Arrays.asList(sa));

// iterate over each element in LinkedList
Iterator iterator = ll.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Convert an Array into a Vector in Java

To convert an Array into a java.util.Vector we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to a Vector of Strings.

import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

/**
* Java 1.4+ Compatible
* The following example code demonstrates converting an Array of Strings to a Vector of Strings
*/
public class Array2Vector {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to Vector
Vector v = new Vector(Arrays.asList(sa));

// iterate over each element in Vector
Iterator iterator = v.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Convert an Array into an ArrayList in Java

To convert an Array into an java.util.ArrayList we can use the java.util.Arrays class's asList method. The following code example demonstrates converting an Array of Strings to an ArrayList of Strings.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
* Java 1.4+ Compatible 

* The following example code demonstrates converting an Array of Strings to an ArrayList of Strings
*/
public class Array2ArrayList {

public static void main(String[] args) {

// initialize array with some data
String[] sa = new String[] { "A", "B", "C" };
// convert array to ArrayList
ArrayList al = new ArrayList(Arrays.asList(sa));

// iterate over each element in ArrayList
Iterator iterator = al.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
}
}



Here is the output of the example code:
A
B
C

Tuesday, July 6, 2010

E to the 0th Power Equals 1

Here's a little mathematical proof of e^0=1 that I had laying around for a while. It's generalized to x^0=1 because anything to the power of zero equals one.



See also: Number Patterns