Showing posts from March, 2017

Format a number as US currency [JavaScript]

A recent project I worked on allowed customers to request cloud computing resources. One of the requirements was to display the cost for each resource amount and the totals for one and two years for the resources the customer was requesting. In the United States, numbers are formatted with a period for the decimal mark and a comma as the thousands separator, e.g. 1,234,567.89.
The function below is going to: Assume that the input is either an unsigned integer or a float, ex. 1234567 or 1234567.90.Insert a "," delimiter every third digit.Optionally, add a currency symbol to the returned string. Let’s review: On lines 2 - 4 I defined some default values.On line 6 I defined a closure called formatNumber. The logic in the closure is going to format the number string.Starting at line 28 I am making sure that when the formatCostNumber function is called it does receive a value to format, and that…

Format MAC Address [JavaScript]

I am currently working on a project which will allow users to register their Wi-Fi enabled, non-web browser enabled, devices on the network. These are devices like printers, Apple TV, and Xbox*. One of the data points that have to be collected from the user is the device MAC address. The project customer wants that address to be properly formatted when they see it in the support ticket.

We have several options. We can format the address either on the back-end after the form has been submitted. Or we can format it on the front end via a separate text field for each character pair, but that is too many fields to handle. A better solution is to use a single field and format the user input at the time of input or upon submit. In those cases, the former is better because the data will already be formatted when the overall form input is being validation after the user clicks the “Submit” button.

We are going to format the user input as it is being provided, thus having proper data when vali…