DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • Automating the Migration From JS to TS for the ZK Framework
  • Mainframe Development for the "No Mainframe" Generation
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?

Trending

  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • Automating the Migration From JS to TS for the ZK Framework
  • Mainframe Development for the "No Mainframe" Generation
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?

Currency Format Validation and Parsing

Muhammad Haris user avatar by
Muhammad Haris
·
Apr. 10, 15 · Interview
Like (0)
Save
Tweet
Share
13.57K Views

Join the DZone community and get the full member experience.

Join For Free

In Java, formatting a number according to a locale-specific currency format is pretty simple. You use an instance of java.text.NumberFormat class by instantiating it through NumberFormat.getCurrencyInstance() and invoke one of the format() methods. Following is a code-snippet from https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

static public void displayCurrency( Locale currentLocale) {

    Double currencyAmount = new Double(9876543.21);
    Currency currentCurrency = Currency.getInstance(currentLocale);
    NumberFormat currencyFormatter = 
        NumberFormat.getCurrencyInstance(currentLocale);

    System.out.println(
        currentLocale.getDisplayName() + ", " +
        currentCurrency.getDisplayName() + ": " +
        currencyFormatter.format(currencyAmount));
}

However, if an application allows users to enter an amount as a string using separators and currency symbols, there is quite a possibility that currency format may not have been followed according to the locale. For example, user can use a wrong thousand or decimal separator, or a wrong currency symbol altogether.

In that case, the application should incorporate a mechanism to first ensure that format is followed and then parse that string to convert it into a proper number in order to perform several mathematical calculations in a currency format independent manner. 

It is important to note that parsing a string using java.text.NumberFormat#public Number parse(String source) method requires that the currency string must contain a value following the locale-specific pattern defined in java.text.DecimalFormat and symbols defined in java.text.DecimalFormatSymbols. If the pattern and/or symbols are not followed, program will throw java.text.ParseException. For example, currency pattern for it_CH = Italian (Switzerland) is ¤ #,##0.00 and grouping separator is ', hence SFr. 1'234.56 is valid while 1,234.56 SFr. is invalid

In order to check the validity of a currency amount before actually parsing it, Apache Commons Validator project's org.apache.commons.validator.routines.CurrencyValidator comes in real handy. All you have to do is to construct its object and call public boolean isValid(String value,Locale locale) method to check whether locale-specific format is followed or not.

Once you are done with validation and found that number is valid, you can then parse the number by calling the parse() method. Following code snippet shows how to validate and parse. Note that the validation is lenient and if currency symbol is not already present in the string, it appends appropriate currency symbol according to the pattern and then validate and parse it.

/**
	 * Converts given item price into a number based on given
	 * currency code.
	 * <p>
	 * It works generically for all currencies supported by Java
	 * </p>
	 * 
	 * @param itemPrice currency amount
	 * @param currencyCode 3-letter ISO country code
	 * @return {@link String} containing stripped price or same as given price
	 *         if parsing failed or formatter couldn't be constructed
	 * @author Muhammad Haris
	 */
	public static Double convertPrice(String itemPrice, String currencyCode) {
		Double itemPriceConverted = null;
		Locale currencyLocale = LocaleUtility
				.getLocaleAgainstCurrency(currencyCode);
		DecimalFormat currencyFormatter = getCurrencyFormatter(currencyLocale);

		if (currencyFormatter != null) {
			itemPrice = appendCurrencySymbol(itemPrice,
					currencyFormatter);

			try {
				Number number = currencyFormatter.parse(itemPrice);
				itemPriceConverted = number.doubleValue();				
			} catch (ParseException e) {
				LOG.error("Failed to parse currency: " + currencyCode
						+ ", value: " + itemPrice + ". " + e.getMessage(), e);
			}

		} else {
			LOG.error("No appropriate formatter found for currency: "
					+ currencyCode + ", value: " + itemPrice + ". ");
		}

		return itemPriceConverted;
	}

	/**
	 * Gets currency formatter against given currency locale
	 * 
	 * @param currencyCode
	 *            {@link String} containing 3 letter ISO currency code
	 * @return {@link NumberFormat} object specialized for the currency or null
	 *         if it couldn't be composed
	 * @author Muhammad Haris
	 */
	public static DecimalFormat getCurrencyFormatter(Locale currencyLocale) {

		if (currencyLocale != null) {
			return (DecimalFormat) NumberFormat
					.getCurrencyInstance(currencyLocale);
		}

		return null;
	}

	/**
	 * Appends appropriate currency symbol to the given price using the pattern
	 * defined in the given currency formatter
	 * 
	 * @param itemPrice
	 *            {@link String} containing price of the item in locale specific
	 *            format
	 * @param currencyFormatter
	 *            {@link DecimalFormat} object containing currency locale
	 *            specific formatting info
	 * @author Muhammad Haris
	 */
	public static String appendCurrencySymbol(String itemPrice,
			DecimalFormat currencyFormatter) {
		String currencySymbol = currencyFormatter.getDecimalFormatSymbols()
				.getCurrencySymbol();
		String pattern = currencyFormatter.toPattern();

		if (!itemPrice.contains(currencySymbol)) {
			if (pattern.startsWith("¤ ")) {
				itemPrice = currencySymbol + " " + itemPrice;
			} else if (pattern.endsWith(" ¤")) {
				itemPrice = itemPrice + " " + currencySymbol;
			} else if (pattern.startsWith("¤")) {
				itemPrice = currencySymbol + itemPrice;
			} else if (pattern.endsWith("¤")) {
				itemPrice = itemPrice + currencySymbol;
			}
		}

		return itemPrice;
	}

Opinions expressed by DZone contributors are their own.

Trending

  • 5 Key Concepts for MQTT Broker in Sparkplug Specification
  • Automating the Migration From JS to TS for the ZK Framework
  • Mainframe Development for the "No Mainframe" Generation
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: