Easy Number Handling in JavaScript with APEX.LOCALE

Easy Number Handling in JavaScript with APEX.LOCALE

ยท

3 min read

Introduction

As a self-confessed JavaScript phobe, my heart sank every time I dealt with numbers in JavaScript. That all changed with the introduction of the apex.locale namespace (JavaScript functions). Specifically, apex.locale.formatNumber and apex.locale.toNumber.

In this post, I will show you how these functions can help you convert formatted APEX Page Item values (e.g., 123,890.23) to JavaScript numbers and convert JavaScript number values back to APEX Page Items with the correct format.

Get a Number from an APEX Page Item

One of the significant challenges for me was converting the value entered by a user in an APEX Page Item (or Interactive Grid) into a number type in JavaScript so that I could then perform math on that number. Essentially, you must assume anything entered into an APEX Page Item, even one with a type of 'Number Field', could have non-numeric characters.

Take the following example where the number is formatted using the format string 9G999G990D00. See the documentation for more on Oracle Format Models.

apex.locale Oracle APEX Functions Example Number Field

apex.locale Oracle APEX Functions Example Number Field

A user could type the following into the above field:

apex.locale Oracle APEX Functions Example Number Field

If you try and use this value directly in a JavaScript math operation, you will not get what you are hoping for. JavaScript sees the APEX Item Value as a string and appends 12 to the end of the string.

apex.locale Oracle APEX Functions Error Using Formatted Number

This is where apex.locale.toNumber comes to the rescue:

apex.locale.toNumber removes the comma and period and returns a Number type value. As the namespace name implies, apex.locale also handles number formatting for other countries. In Germany, for example, the '.' is the thousand separator, and the ',' is the decimal point. There are even functions to tell you what the thousand separator, the decimal point, and the currency symbol are for the current session.

apex.locale Oracle APEX Functions. Get locale information.

Unfortunately, it will not strip out other non-numeric values. For example, the following still gives you an error:

apex.locale Oracle APEX Functions Number with Alpha Characters

apex.locale Oracle APEX Functions Using Function

Given this, you must still wrap your code with the JavaScript isNaN function.

if (isNaN(apex.locale.toNumber(
            apex.item( "P10_NUMBER_FIELD" ).getValue(),
            '9G999G990D00'))) {
  console.log('Invalid Number Entered');
} else {
  jdNum = apex.locale.toNumber(
            apex.item("P10_NUMBER_FIELD" ).getValue(),
            '9G999G990D00') + 12;
}

Populate a Page Item with a Number

Once you finish your math operation, you will likely want to display the result to the user. This could be in another APEX Page Item, an Alert Dialog, or something else. You will also likely want to display the result in the same format as the input number. This is where apex.locale.formatNumber comes into play.

The following code:

apex.locale Oracle APEX Functions. Code to convert back to APEX Item.

Results in an appropriately formatted number for your locale:

apex.locale Oracle APEX Functions. Result from converting back to APEX Item.

Conclusion

Just as with SQL and PL/SQL, performing implicit conversions of numbers (or dates, for that matter) is never a good idea. apex.locale.toNumber and apex.locale.formatNumber get us a step closer to performing explicit number conversions in JavaScript.

Essentially, you can think of apex.locale.toNumber and apex.locale.formatNumber like the TO_NUMBER and TO_CHAR Oracle SQL functions, respectively.

๐ŸŽฅ Video

Read More

I encourage you to check out my post, Easy Date Handling in JavaScript with apex.date. This post talks about how apex.locale and apex.date help you to perform explicit date conversions in JavaScript.

- ๐Ÿฉณ APEX Shorts

- ๐Ÿ“ Other APEX Posts

ย