Join the Discussion
Wouldn't it be nice to be able to properly format numbers before displaying them? Actually it is not that difficult and there are a number of different ways to do it. The way that I selected uses a couple of Math methods and some array manipulations to produce the required formatting allowing for just about all of the location based variations to how you might want to display numbers that I can think of.
Now when you want a nicely formatted number you just call the format number function before displaying the number. For example, let's say that we want to display the number with two decimal places using spaces for thousands separators (a common international standard) and a period for the decimal point. It isn't a monetary value so we'll leave the currency indicators blank. If the number is negative we'll keep the - on the front. If our number field is mynum then we would just replace the reference to mynum that is displaying with the following to display our formatted version.
We can easily select different formatting by changing the values in the second through eighth parameters. The second parameter is the number of decimal places that the number should have. If the number contains more decimal places than required it will be rounded to the nearest number with that number of decimal places. If it has fewer decimal places than specified zeroes will be added to the end.
The third parameter is the thousands separator. In our example we used a space but a comma or period may be what is required for your location.
The fourth parameter is the decimal point. Either a period or comma is normal here.
The fifth and sixth parameters are used for monetary values and one or other of them will contain the currency symbol when required. If your location uses a currency symbol hard against the left of numbers then place that symbol by itself in the fifth parameter eg. '$'. If you normally have a space after the currency symbol then add it after the symbol in this '$ '. If your currency symbol comes after the amount instead of before then place it in the sixth parameter instead of the fifth parameter.
The seventh and eighth parameters define the symbols to place around the number when the value is negative. The usual values for these parameters would be '-','' but you may have a situation where you want to use '(',')' or even '',' CR'.
We'll look at one more example to finish off. This time we want two decimal places, commas for thousands separators, a period for a decimal point, a £ symbol followed by a space for the currency and a CR suffix for negative amounts.
Of course you may prefer to assign any of these values to variables and pass those variables to the function instead which would make keeping track of which parameter is which much easier.