Home Tutorial Java Corejava Javatext Formatting and Parsing Locale-Specific Currency

 
 

Share on Google+Share on Google+
Formatting and Parsing Locale-Specific Currency
Posted on: October 11, 2010 at 12:00 AM
Advertisement
In this section, you will learn how to format and parse the locale specific currency.

Formatting and Parsing Locale-Specific Currency

In this section, you will learn how to format and parse the locale specific currency.

Through the formatting, you can translate the binary data into user-readable textual representation of the values. This is not a difficult task. By providing Formatters like, NumberFormat, Decimal format etc, Java has made programming easier. Here we are going to format the number into locale specific currency using NumberFormat class. This class is a locale sensitive which format the primitive type numbers into a locate specific string.

In the given example, we have specified a locale 'FRANCE' and invoked getCurrencyInstance() method to create a formatter. This method returns a string with the formatted number and the appropriate currency sign and the method format() formats the number. For parsing, we have used parse() method which parse the text to produce a number.

Here is the code:

import java.text.*;
import java.util.*;

public class FormattingAndParsingUsingLocaleSpecificCurrency {
	public static void main(String[] args) {
		Locale locale = Locale.FRANCE;
		String st = NumberFormat.getCurrencyInstance(locale).format(123.45);
		System.out.println(st);
		try {
			Number number = NumberFormat.getCurrencyInstance(locale).parse(st);
			System.out.println(number.doubleValue());
		} catch (Exception e) {
		}

	}
}

Output:

123,45 ?
123.45

Advertisement

Related Tags for Formatting and Parsing Locale-Specific Currency:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 11, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com