Substraction of next amount from previous amount.

FreakyFreaky Member Posts: 125
Hi Guys, can anyone help me on how I can get the differences between amounts.For example I have a variable called
amount
56
86

so i will like to have an answer of 86-56=30.

Thanks in advance.

Comments

  • jversusjjversusj Member Posts: 489
    perhaps try giving your variable dimensions and make an array - then you'd have to add code to move the value of amount[1] to amount [2] on each iteration and then you could subtract [1] from [2]... does that get you started?
    kind of fell into this...
  • FreakyFreaky Member Posts: 125
    Jversusj thanks for the help but I have no idea on how to use arrays in Nav. And my values on the variable "amount" is not just two they are numerous. Can you please assist more.
  • DenSterDenSter Member Posts: 8,304
    MyAmount := 86 - 56;
    MESSAGE('The amount is ' + FORMAT(MyAmount));
    
  • FreakyFreaky Member Posts: 125
    Am Sorry if I did not give you a clear picture of what I meant this is how it is

    Amount(decimal)
    56.00
    86.00
    86.00
    357.00
    389.00
    413.00
    480.00
    it continues


    Results
    0
    86-56
    86-86
    357-86
    389-357
    413-357
    480-413
    it continues

    So in each case I want to subtract the first value from the second value.

    Thanks.
  • jversusjjversusj Member Posts: 489
    i think you can do this, like i said, with an array.

    create your variable and look at the properties. Set the dimensions property to 2 - this will let you keep 2 values in the variable at a time: variable[1] and varibable [2]. then you would do your processing. on your first record (you could be clever to code for the first time it happens versus all subsequent times), you would set variable[2] = your value. variable[1] would be 0. you could then do variable[2] - variable[1].

    for all subsequent records you would first say variable[1] := variable[2], then you would set variable[2] equal your new value. you could then do variable[2] - variable[1] as before and get the difference.

    so, using your example values:
    56.00
    86.00
    86.00
    357.00
    389.00
    413.00
    480.00
    etc.

    the first time through, set variable[2] := 56.00. there is no previous entry because you just started, so difference := variable[2] - variable[1] would be 56.00. you could code a different formula if you really want the answer to be 0 (according to your example).
    the next record would set variable[1] := 56.00 and variable[2] := 86.00, then difference := variable[2] - variable[1].

    you would continue this through all records and always subtract the current from the previous.
    kind of fell into this...
  • MBergerMBerger Member Posts: 413
    If it's on a form, then you could do the following :
    - Add a Global Variable for your difference ( I'l call it Difference )
    - Add a textbox to your tablebox, and add the Difference variable as its SourceExpression
    - In the OnAfterGetRecord of the form, declare a variable of the same type as the main record ( let's call it PrevRec )
    Prevrec.copy(rec) ;
    if PrevRec.next(-1) <> 0 then
      //No previous Record found, assume 0 as previous value
      PrevRec.init ;
    
    Difference := Rec.Value - Prevrec.value ;
    

    Oh, and do consider that this will look strange if the records aren't sorted on the value you want to use, and filters might do strange things.
  • BeliasBelias Member Posts: 2,998
    boolfirst := true
    repeat
      actualvalue = tb.amount
      if BOOLfirst then begin
        BOOLfirst := false;
        result := 0;
      end else begin
        result := actualvalue - previousvalue;
      end;
      previousvalue := tb.amount;
    until tb.next = 0;
    

    ***Not tested but should work :mrgreen:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • FreakyFreaky Member Posts: 125
    Thanks guys for the help. I finally succeeded by using the array method jversusj posted.
  • jversusjjversusj Member Posts: 489
    good to hear, although you should note that belias' code does the same thing, without messing with arrays. you would just set up two variables instead of 1 variable with multiple dimensions.
    kind of fell into this...
  • BeliasBelias Member Posts: 2,998
    jversusj wrote:
    good to hear, although you should note that belias' code does the same thing, without messing with arrays. you would just set up two variables instead of 1 variable with multiple dimensions.
    but at least he learnt arrays...which is good! :mrgreen::mrgreen:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • FreakyFreaky Member Posts: 125
    Belias I try using your code and when I try compiling it, it says THERE IS A SYNTAX ERROR.

    Thanks
  • BeliasBelias Member Posts: 2,998
    Freaky wrote:
    Belias I try using your code and when I try compiling it, it says THERE IS A SYNTAX ERROR.

    Thanks
    Come on! don't blame me so much! :mrgreen: (joking)...i forgot a semicolon...hmmm 2 semicolon, 1st and 3rd line...i didn't compile the code pefore posting it... 8)
    ....and put a findset or find('-') before the repeat :whistle:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • FreakyFreaky Member Posts: 125
    :mrgreen: Thanks once more.
Sign In or Register to comment.