Topic Options
#57871 - 02/05/21 12:27 PM Totaling iteration values but needing commas
dweg Offline
OL Expert

Registered: 11/05/08
Posts: 162
Loc: Denver
Hello All,

I've created a function (below) that iterates through a field to create a total value. Problem is I need the output to have separating commas as in currency.

I'm trying to adapt an amount function from the PlanetPress Talk libraries, specifically @FormatAmountCurr into it. Although I'm thinking I may not be able to put a function call inside of a function? And placing everything into one function seems over my head.

Any help would be appreciated.

function @OFFER_PRIC()
Define(&total, currency, 0)
Define(&i, integer, 0)
for(&i, 1, 1, subreccount())
&total:= &total + strtocur(Field('OFFER_PRIC', &i))
endfor()
show(curtostr(&total))
endfunction()

Top
#57872 - 02/05/21 02:51 PM Re: Totaling iteration values but needing commas [Re: dweg]
jim3108 Online   content
OL Expert

Registered: 04/19/10
Posts: 311
Loc: London, UK
Usually, I'd have my function return a value and then show that in the place that I want it and call the format function to correct the output but doing it your way, there's no reason why you couldn't say:
Code:
show(curtostr(@FormatAmountCurr(Field('OFFER_PRIC', &i))))

Just make sure that FormatAmountCurr is listed first in the functions list.

Best regards,

James.

Top
#57897 - 02/24/21 10:14 AM Re: Totaling iteration values but needing commas [Re: dweg]
dweg Offline
OL Expert

Registered: 11/05/08
Posts: 162
Loc: Denver
Oh Jim3108, I wish it was that easy for me!

I want to use the FormatAmountCurr yes, although the code to create the iteration of line items makes it so the total data is not in my OFFER_PRIC data field. It's looping through every child record for that recipient to total it up and ends up in &total.

I was hoping I could take my function @OFFER_PRIC() and combine it with the FormatAmountCurr function. Although I'm having no luck.

Any help would be appreciated.

Top
#57898 - 02/25/21 06:22 AM Re: Totaling iteration values but needing commas [Re: dweg]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1969
Loc: Objectif Lune, Montreal, Qc
As much as possible, functions should not display anything, they should simply return a value which you can then use inside one of the objects in your template.

Assuming your current code produces the proper numeric value, you could modify it so it looks like this:
Code:
function @OFFER_PRIC(): string
  Define(&total, currency, 0)
  Define(&i, integer, 0)
  for(&i, 1, 1, subreccount())
    &total:= &total + strtocur(Field('OFFER_PRIC', &i))
  endfor()
  &result:=@FormatAmountCurr(&total,',')
endfunction()

Then in the calling object, simply use:
Code:
show(@OFFER_PRIC())

But being a programmer at heart, I don't much like this because the OFFER_PRIC function then performs two separate tasks: totaling fields AND formatting the result.

My preferred way of doing this would be:
Code:
function @OFFER_PRIC(): currency
  Define(&total, currency, 0)
  Define(&i, integer, 0)
  for(&i, 1, 1, subreccount())
    &total:= &total + strtocur(Field('OFFER_PRIC', &i))
  endfor()
  &result:=&total
endfunction()

Now, the funtion does only one thing: it returns the total value as a currency.
You can then use that value anywhere you need the total, and when you want to display it in a formatted manner, you would use:
Code:
show(@FormatAmountCurr(@OFFER_PRIC(),','))

This way, each function performs a single task and nesting them as shown above allows you to use different combinations.

Note: make sure that you have imported both the FormatAmountCurr and FormatAmountStr global functions as the former depends on the latter.
_________________________
Technical Product Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top
#57899 - 02/26/21 12:04 PM Re: Totaling iteration values but needing commas [Re: dweg]
dweg Offline
OL Expert

Registered: 11/05/08
Posts: 162
Loc: Denver
Thank you Philippe. I agree with your choice of splitting the tasks between two different functions, just wasn't sure how to achieve that. Now I can use the iteration total differently somewhere else if I like as well.

I used the show command inside a text box and it works great!

You guys rock! I appreciate it.

Top