Javascript calculations

Use this board to ask questions or have discussions with other Rich Displays users.
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post by ppbedz »

Glenn,

How can I get the following to edit with commas? pui.set("TotGridTot.1", "$" + totLab.toFixed(2));

Thank you,

Patti
User avatar
Glenn
Profound Logic Staff Member
Posts: 124
Joined: Mon Apr 14, 2014 4:08 pm
First Name: Glenn
Last Name: Hopwood
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Javascript calculations

Post by Glenn »

Patti,

Perhaps the toLocaleString() method would work? I did a quick test (that didn't involve a grid) and it worked in Chrome.

Try this (Note that it rounds if your decimal positions are > 2):

Code: Select all

 pui.set("TotGridTot.1", totLab.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));
Glenn
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post by ppbedz »

Sorry, I don't get any commas. My grid field is defined as character. The rpg initially formats the field - s2totalc = '$' + %trim(%editc(s2total:'2'));

The javascript code comes into play if the user filters. Then I recalculate and reload the totals in javascript, but the commas disappear. No one has said anything about it, but I thought it would be nice if the format stayed consistent.
User avatar
Glenn
Profound Logic Staff Member
Posts: 124
Joined: Mon Apr 14, 2014 4:08 pm
First Name: Glenn
Last Name: Hopwood
Company Name: Profound Logic Software
State / Province: Ohio
Country: United States
Contact:

Re: Javascript calculations

Post by Glenn »

Patti,

When you say "My grid field is defined as character" do you mean "TotGridTot.1"? If so, that shouldn't matter since that is where the value is going to be placed, not where it's coming from. As long as "totLab" is a numeric value it should work (at least it did in my testing).

If "totLab" contains "$" and "," characters you could use the .replace() method to strip them off before you send it through the toLocaleString() method. It would look something like this...

Code: Select all

// Convert $12,123,123.50 to 12123123.50
var totLabNum = Number(totLab.replace(/[$,]+/g,""));
// Set "TotGridTot.1" to the value of "totLabNum" with a "mask" of US dollar currency ($ and , and .)
pui.set("TotGridTot.1", totLabNum.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));
Glenn
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Javascript calculations

Post by Scott Klement »

You don't get commas using toLocaleString() (like Glenn posted)? Are you using a very old browser? ToLocaleString() requires IE11, Firefox 29, and Chrome 24. These have all been around for several years now.

Note that the formatting from your RPG program does not matter, as you are setting the value from JavaScript with pui.set(). It is only the formatting that you're providing in the pui.set() statement that matters.
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post by ppbedz »

I am using IE 11. I understand about the js formatting. I was just acknowledging that the initial load by the rpg is correct, and the js changes the format. I understand why that happens. I am enclosing my function.
function engwkrqCalcTotal() {
var data = getObj("ReqGrid").grid.getAllDataValues(true);
var totLab = 0;
var totCsm = 0;
var totMsc = 0;
var totCst = 0;
var totSmp = 0;

for (var i=0; i<data.length; i++) {
totLab += Number(data["S2LABCST"]);
totCsm += Number(data["S2CSMCST"]);
totMsc += Number(data["S2MSCCST"]);
totCst += Number(data["S2TOTCST"]);
totSmp += Number(data["S2SMPQTYI"]);
}
pui.set("TotGridTot.1", "$" + totLab.toFixed(2));
pui.set("TotGridTotC.1", "$" + totLab.toFixed(2));

pui.set("TotGridTot.2", "$" + totCsm.toFixed(2));
pui.set("TotGridTotC.2", "$" + totCsm.toFixed(2));

pui.set("TotGridTot.3", "$" + totMsc.toFixed(2));
pui.set("TotGridTotC.3", "$" + totMsc.toFixed(2));

pui.set("TotGridTot.4", "$" + totCst.toFixed(2));
pui.set("TotGridTotC.4", "$" + totCst.toFixed(2));

pui.set("TotGridTot.5", totSmp.toFixed(0));
pui.set("TotGridTotC.5", totSmp.toFixed(0));
}
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Javascript calculations

Post by Scott Klement »

You don't appear to be using the toLocaleString() examples that Glenn posted at all?
ppbedz
Experienced User
Posts: 147
Joined: Tue Jun 17, 2014 4:00 pm
First Name: Patti
Last Name: Bednarz
Company Name: McGard
State / Province: New York
Country: United States
Contact:

Re: Javascript calculations

Post by ppbedz »

I sent you the code before I tried Glenn's example. Also, I am sorry. Glenn's first examples "does" work (I don't need the "replace"). I only changed one of my grid rows to test it. However, I changed the wrong the statement. Thank you both! Patti
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest