Jump to content
Salesforce and other SMB Solutions are coming soon. ×

How to get the Sum of 2 numbers in the same field


Plinio
 Share

Recommended Posts

Is the number always going to be 2 digits? There are a couple ways to do this.

Do you have FileMaker Pro 8? Or FileMaker Pro Advanced 8? ( Advanced lets you create custom functions ) If you don't have Advanced, and the numbers are variable in length, it can be a little more challenging to do this task because you can't take advantage of recursion available to custom functions. It can be done, it just requires a larger function with a lot of wasted cycles.

If you have advanced, you can use a custom function like this:

/**
 *
 * sumDigits ( number )
 * @description sum the individual digits from the provided number. Example, returns 12 when provided the number 4314 ( 4 + 3 + 1 + 4 ).
 *
 * @param {number} [number] the number to parse and sum.
 *
 * @returns {object} number - returns a sum of the individual digits.
 *
 * @history 2016-04-17 Josh Ormond - created.
 *
 */


Case ( 
	number ≤ 9 ; 
		number ;

	Mod ( number ; 10 ) + sumDigits ( Floor ( number / 10 ) )

)

 

 

If you don't have FileMaker Pro Advanced ( or version 18+ ), you could use something like below. You will need a corresponding row for each digit up to your max. If you have numbers up to 12 digits long, you will need additional rows, 12 of them, and adjust the result row to match. Again, this can give you a lot of wasted computer cycles.

Let ( 

[

	number = 4321 ; 
	digit_1 = Middle ( number ; 1 ; 1 ) + 0 ; 
	digit_2 = Middle ( number ; 2 ; 1 ) + 0 ; 	
	digit_3 = Middle ( number ; 3 ; 1 ) + 0 ; 	
	digit_4 = Middle ( number ; 4 ; 1 ) + 0 ; 	
	result = Sum ( digit_1 ; digit_2 ; digit_3 ; digit_4 )

] ; 

	result

)

 

Link to comment
Share on other sites

For reference sake, and for others reading this later:

  • both calcs will provide the correct answer.
  • the custom function you create with the first calc is more robust. You won't have to waste cycles on calculating digits that don't exist. It is, from a computer science perspective, the better approach. But it also requires FileMaker Pro Advanced to create a new custom function in the file. In this case, the OP doesn't have advanced. Future versions ( 18+ ) open this functionality to all users. There is no longer Pro and Advanced. It's a single product.
  • the first approach uses recursion, which requires the calc to be used in a custom function.
  • in modern versions of FileMaker, there is also the While ( ) function. That would allow this to be handled differently, and would not require recursion specifically. Although, in a version where While ( ) exists, everyone with a Full Access account can access the custom functions.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

Terms of Use