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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Validate Names Using Java
  • High-Performance Java Serialization to Different Formats
  • Java Memory Management
  • A Spring Boot App With Half the Startup Time

Trending

  • How to Migrate From LangChainGo to Genkit Go
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • AI Agents and Frontier Models: Insights From The Neuron

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Currency Format Validation and Parsing

Currency Format Validation and Parsing

By 
Muhammad Haris
·
Apr. 10, 15 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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 Spring Boot App With Half the Startup Time

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook