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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Validate Names Using Java
  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • A Systematic Approach for Java Software Upgrades

Trending

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Streamlining Event Data in Event-Driven Ansible
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Emerging Data Architectures: The Future of Data Management
  1. DZone
  2. Coding
  3. Java
  4. Currency Format Validation and Parsing

Currency Format Validation and Parsing

By 
Muhammad Haris user avatar
Muhammad Haris
·
Apr. 10, 15 · Interview
Likes (0)
Comment
Save
Tweet
Share
14.5K 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;
	}
Strings application Locale (computer hardware) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How To Validate Names Using Java
  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • A Systematic Approach for Java Software Upgrades

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!