Java I18N – DecimalFormatSymbols Class

Java Internationalization – DecimalFormatSymbols Class ”; Previous Next Using DecimalFormatSymbols class, the default separator symbols, grouping separator symbols etc. can be changed. Following example is illustrating the same. Example import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; public class I18NTester { public static void main(String[] args) { String pattern = “#,###.###”; double number = 126473.4567; DecimalFormat decimalFormat = new DecimalFormat(pattern); System.out.println(decimalFormat.format(number)); DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(); decimalFormatSymbols.setDecimalSeparator(”;”); decimalFormatSymbols.setGroupingSeparator(”:”); decimalFormat = new DecimalFormat(pattern, decimalFormatSymbols); System.out.println(decimalFormat.format(number)); } } Output It will print the following result. 126,473.457 126:473;457 Print Page Previous Next Advertisements ”;

Java I18N – Grouping Digits

Java Internationalization – Grouping Digits ”; Previous Next Using setGroupingSize() method of DecimalFormat, default grouping of numbers can be changed. Following example is illustrating the same. Example import java.text.DecimalFormat; public class I18NTester { public static void main(String[] args) { double number = 121223232473.4567; DecimalFormat decimalFormat = new DecimalFormat(); System.out.println(number); System.out.println(decimalFormat.format(number)); decimalFormat.setGroupingSize(4); System.out.println(decimalFormat.format(number)); } } Output It will print the following result. 1.212232324734567E11 121,223,232,473.457 1212,2323,2473.457 Print Page Previous Next Advertisements ”;

Java I18N – Parsing Numbers

Java Internationalization – Parse Numbers ”; Previous Next Example In this example, we”re showcasing parsing of number present in different locale. import java.text.NumberFormat; import java.text.ParseException; import java.util.Locale; public class I18NTester { public static void main(String[] args) throws ParseException { Locale enLocale = new Locale(“en”, “US”); Locale daLocale = new Locale(“da”, “DK”); NumberFormat numberFormat = NumberFormat.getInstance(daLocale); System.out.println(numberFormat.parse(“100,76”)); numberFormat = NumberFormat.getInstance(enLocale); System.out.println(numberFormat.parse(“100,76″)); } } Output It will print the following result. 100.76 10076 Print Page Previous Next Advertisements ”;

Java I18N – Formatting Patterns

Java Internationalization – Formatting Patterns ”; Previous Next Followings is the use of characters in formatting patterns. Sr.No. Class & Description 1 0 To display 0 if less digits are present. 2 # To display digit ommitting leading zeroes. 3 . Decimal separator. 4 , Grouping separator. 5 E Mantissa and Exponent separator for exponential formats. 6 ; Format separator. 7 – Negative number prefix. 8 % Shows number as percentage after multiplying with 100. 9 ? Shows number as mille after multiplying with 1000. 10 X To mark character as number prefix/suffix. 11 ” To mark quote around special characters. Example In this example, we”re formatting numbers based on different patterns. import java.text.DecimalFormat; public class I18NTester { public static void main(String[] args) { String pattern = “###.###”; double number = 123456789.123; DecimalFormat numberFormat = new DecimalFormat(pattern); System.out.println(number); //pattern ###.### System.out.println(numberFormat.format(number)); //pattern ###.# numberFormat.applyPattern(“###.#”); System.out.println(numberFormat.format(number)); //pattern ###,###.## numberFormat.applyPattern(“###,###.##”); System.out.println(numberFormat.format(number)); number = 9.34; //pattern 000.### numberFormat.applyPattern(“000.##”); System.out.println(numberFormat.format(number)); } } Output It will print the following result. 1.23456789123E8 123456789.123 123456789.1 123,456,789.12 009.34 Print Page Previous Next Advertisements ”;

Java I18N – Home

Java Internationalization Tutorial Quick Guide Resources Job Search Discussion Internationalization or I18N refers to the capablity of an Application to be able to server users in multiple and different languages. Java has in-built support for Internationalization. Java also provides formating of numbers, currecies and adjustment of date and time accordingly. This tutorial covers most of the topics required for a basic understanding of Java Internationalization and to get a feel of how it works. Audience This tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to Java Internationalization. Prerequisites Before you start practicing various types of examples given in this reference, we assume that you are already aware about computer programs and computer programming languages. Print Page Previous Next Advertisements ”;

Java I18N – DateFormatSymbols Class

Java Internationalization – DateFormatSymbols Class ”; Previous Next Using DecimalFormatSymbols class, the default separator symbols, grouping separator symbols etc. can be changed. Following example is illustrating the same. Example import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; public class I18NTester { public static void main(String[] args) { String pattern = “#,###.###”; double number = 126473.4567; DecimalFormat decimalFormat = new DecimalFormat(pattern); System.out.println(decimalFormat.format(number)); DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(); decimalFormatSymbols.setDecimalSeparator(”;”); decimalFormatSymbols.setGroupingSeparator(”:”); decimalFormat = new DecimalFormat(pattern, decimalFormatSymbols); System.out.println(decimalFormat.format(number)); } } Output It will print the following result. 126,473.457 126:473;457 Print Page Previous Next Advertisements ”;

Java I18N – From and To String Conversion

Java Internationalization – Unicode Conversion from/to String ”; Previous Next In java, text is internally stored in Unicode format. If input/output is in differnt format then conversion is required. Conversion Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[]. Example import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.text.ParseException; public class I18NTester { public static void main(String[] args) throws ParseException, UnsupportedEncodingException { String unicodeString = “u00C6u00D8u00C5” ; //convert Unicode to UTF8 format byte[] utf8Bytes = unicodeString.getBytes(Charset.forName(“UTF-8”)); printBytes(utf8Bytes, “UTF 8 Bytes”); //convert UTF8 format to Unicode String converted = new String(utf8Bytes, “UTF8”); byte[] unicodeBytes = converted.getBytes(); printBytes(unicodeBytes, “Unicode Bytes”); } public static void printBytes(byte[] array, String name) { for (int k = 0; k < array.length; k++) { System.out.println(name + “[” + k + “] = ” + array[k]); } } } Output It will print the following result. UTF 8 Bytes[0] = -61 UTF 8 Bytes[1] = -122 UTF 8 Bytes[2] = -61 UTF 8 Bytes[3] = -104 UTF 8 Bytes[4] = -61 UTF 8 Bytes[5] = -123 Unicode Bytes[0] = -58 Unicode Bytes[1] = -40 Unicode Bytes[2] = -59 Print Page Previous Next Advertisements ”;

Java I18N – From Reader and To Writer Conversion

Unicode Conversion from/to Reader/Writer ”; Previous Next Reader and Writer classes are character oriented stream classes. These can be used to read and convert Unicode characters. Conversion Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[] using Reader and Writer classes. Example import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.Charset; import java.text.ParseException; public class I18NTester { public static void main(String[] args) throws ParseException, IOException { String input = “This is a sample text” ; InputStream inputStream = new ByteArrayInputStream(input.getBytes()); //get the UTF-8 data Reader reader = new InputStreamReader(inputStream, Charset.forName(“UTF-8”)); //convert UTF-8 to Unicode int data = reader.read(); while(data != -1){ char theChar = (char) data; System.out.print(theChar); data = reader.read(); } reader.close(); System.out.println(); //Convert Unicode to UTF-8 Bytes ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(outputStream, Charset.forName(“UTF-8″)); writer.write(input); writer.close(); String out = new String(outputStream.toByteArray()); System.out.println(out); } } Output It will print the following result. This is a sample text This is a sample text Print Page Previous Next Advertisements ”;

Java I18N – Locale Specific DecimalFormat

Java Internationalization – Locale Specific DecimalFormat ”; Previous Next By default, DecimalFormat object is using the JVM”s locale. We can change the default locale while creating the DecimalFormat object using NumberFormat class. In the example below, we”ll use same pattern for two different locale and you can spot the difference in the output. Example import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; public class I18NTester { public static void main(String[] args) { String pattern = “###.##”; double number = 123.45; Locale enlocale = new Locale(“en”, “US”); Locale dalocale = new Locale(“da”, “DK”); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(enlocale); decimalFormat.applyPattern(pattern); System.out.println(decimalFormat.format(number)); decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(dalocale); decimalFormat.applyPattern(pattern); System.out.println(decimalFormat.format(number)); } } Output It will print the following result. 123.45 123,45 Print Page Previous Next Advertisements ”;

Java I18N – Set Rounding Mode

Java Internationalization – Set Rounding Mode ”; Previous Next Example In this example, we”re showcasing Rounding Mode. import java.math.RoundingMode; import java.text.NumberFormat; import java.util.Locale; public class I18NTester { public static void main(String[] args) { Locale enLocale = new Locale(“en”, “US”); NumberFormat numberFormat = NumberFormat.getInstance(enLocale); numberFormat.setMinimumFractionDigits(0); numberFormat.setMaximumFractionDigits(0); System.out.println(numberFormat.format(99.50)); numberFormat.setRoundingMode(RoundingMode.HALF_DOWN); System.out.println(numberFormat.format(99.50)); } } Output It will print the following result. 100 99 Print Page Previous Next Advertisements ”;