Using Javascript to format numbers

Sep 30, 2013

Am trying to format numbers for a financial course. Found a function and see where I can execute Javascript as a trigger, but not knowing how javascript works, I'm not sure how to USE this function to get my variable formatted and then displayed. Ideas please? Thx...

10 Replies
Michael Hinze

Carrie Eaton said:

Thanks, Michael....I've seen this article but since I know NOTHING about Javascript, I'm not sure where to plug in my variable name in the function....or does it go in the function?? Do I enter the function in the trigger and then somehow call that function with my variable name, which has already been defined?


Maybe you can post your file and the JavaScript function here for the community to have a look?

Carrie Eaton

Absolutely! I'm hoping you don't need a file.....it's for a corporate client so I can't easily share.

I've got a calculator where the learner can enter their salary and the % they are contributing to their 401(k) and then click one of three buttons to calculate the company match, annual totals, etc. The company match is based on company performance so the buttons are minimum, target and maximum so that they can see the difference comapny performance has on the match.

One variable is CompanyMatchPerPayPeriod and I want to format that to currency as the last trigger so the amount displays with two decimals and commas.

Here's the function:

Number.prototype.formatMoney = function (c, d, t) {
                var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n 3 ? j % 3 : 0;
                return s + '$' + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(d{3})(?=d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
            };

So now I have the pieces but am not sure how to plug my variable in and get it back!?
Carrie Eaton

I just realized that much of this function got cut off as I pasted it....but I'm assuming you don't need the full function to answer my question?

From the article I read earlier, looks like I can place this function in a .js file in the story_content folder of the published file and then call it from within my course. Am assuming that I will call it with an argument, i.e., my variable name, and this call is in the trigger 'execute Javascript'.  Do I need to do anything specific to get the formatted value 'back' or will the function format what I send it and return it formatted without intervention from me?

I would have thought I'd call it with formatMoney(CompanyMatchPerPayPeriod) but I wonder about the 'Number.prototype.'

Sorry for being such a newbie, but it seems like I'm very close....appreciate your help much!

Carrie Eaton

Just saw this on the site with the function....

BTW, you can use this in any standard JavaScript call, even in Underscore templates like this:


 

So it appears the argument should be the number of decimals and the separator to use, not the variable name.

And I am not married to this particular function. If anyone is aware of anything simpler/better, I'm open to any suggestions and am

certainly willing to purchase a function and perhaps an hour of someone's time to get this done.

As an aside, surely I'm not the only one who needs numbers formatted!? And, yes I have submitted a feature request!

This discussion is closed. You can start a new discussion or contact Articulate Support.