Press "Enter" to skip to content

Oracle Apex: Format Number with Comma and Decimal Using JavaScript

In this Oracle Apex tutorial, you will learn how to format a number with comma and decimal using JavaScript. In Oracle Apex, for a text field or a number field, there is a property Format Mask, in which you can specify the number format with comma and decimal for the currency fields, but it shows after the page load and usually we require it to show when user moves to the next field by pressing the Tab key or using the mouse. To achieve this functionality in Oracle Apex, we can use the JavaScript method Number(value).toFixed() on the onfocusout or onblur event. Below are the examples:

Format Number with Comma and Decimal Using JavaScript Example

In the following example, we will use the JavaScript method Number(value).toFixed().replace() on focusout event to format the number into the currency format. Follow these steps:

Click on the Number Field in Oracle Apex page designer, for which you want to apply the format mask, then in the properties advance section paste the following JavaScript code in the Custom Attributes. Also, shown below in the image below:

onfocusout="this.value=Number(this.value).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')"

Oracle Apex - Convert number to currency format

Now when the user will press the Tab key or will use the mouse to navigate to the next field, this field value will be formatted with commas and 2 decimal places. If you want to change it to show 4 decimal places then use toFixed(4).

JavaScript to Format a Number with 2 Decimal Places in Oracle Apex

To format a number with 2 decimal places without the comma, use the following JavaScript code in the Customer Attributes section:

onfocusout="this.value=Number(this.value).toFixed(2)"

Here in both examples, I used the onfocusout JavaScript event, you can also use the onblur JavaScript event for the same code. Below is the example:

onblur="this.value=Number(this.value).toFixed(2)"

Related Tutorials:

Have you found the answer to your question? If not, you can discuss it with me in the comments section below or join my Q&A community OrclQA.com for developers and ask your question. It is FREE.

Vinish Kapoor

Follow

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

guest
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Naren
Naren
7 months ago

Hi Vinish,
If it’s numeric field, it is returning error with field must be numeric

ROEL CANTU
ROEL CANTU
6 months ago

How can I divide the values of a column (Ex. 00000048 / 10000) in same column? I’ve made it work in the SELECT statement, but when I try to save changes, the error message says that “Virtual Columns are not allowed”.

Phrog
Phrog
5 months ago

Hi Vinish,
I use Android device to add Decimal number in item. I use inputmethod=”decimal” to get numeric keyboard. My problem is that semicoln on keyboard is not working properly: comma is typed instead. Do you have other fix than simply replace comma after lose-focus on item?

Eduardo
Eduardo
5 months ago

I tried but it did not work on my application. If I try the amount 18.211,32 or 18211,32 or 18,211.32 it returns an error. If only works if I try 18211.32, then it converts to 18,211.32.

My field is a numeric one with the mask you recomended (999G999G999G999G990D00).
The problem is on the onfocusout which is not “understanding” the comma if the user paste or type on the field.

The correct is to replace comma for dot and the dot for comma. As I am in Brasil where the format is 0.000,00, the user, usually copy the amount from an document and paste directly to the form. But I have to convert do USD which use the forma 0,000.00.

How can I do it? I already tried an SQL function replace but without sucess.

Richa
Richa
10 hours ago

How about phone number mask 999-999-9999 ?