Formatting and Parsing Locale-Specific Currency


 

Formatting and Parsing Locale-Specific Currency

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

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

Ads