Brian Dunning's FileMaker Custom Functions

FormatPercent ( Number ; Decimals )

Formats a number as a percentage text string with specified number of decimal places

  Average rating: 4.5 (35 votes) Log in to vote

Will Loving   Will Loving
Dedication Technologies, Inc.
http://studioschoolpro.com

Share on Facebook Share on Twitter

  Sample input:
FormatPercent ( .4587 ; 1 )
  Sample output:
45.9%

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

Formats a number value as a percentage in a text string with the specified number of decimal places, from 0 to 10. Useful for calculated text strings that need to include a formatted percentage.

17 Jan 2011 - modified to address issue noted by Jeremiah Hammond.

 

Comments

Jeremiah Hammond   Jeremiah Hammond, Indianapolis
Jan 17, 2011
The current version (as of 01/17/2011) fails with the following input: FormatPercent( .1304 ; 1 ). It'll return 1300% instead of 13.0%. The issue has to do with the ending case statement, where the rounded percent - integer of the rounded percentage happens to equal 0, instead of some decimal (say: 13.0 - 13). The function will attach zeros to the whole number portion of the percentage without a decimal.

Here's a fixed version of the function:

Case ( Decimals = 0 or IsEmpty ( Decimals ) ; Int ( Round ( Number * 100 ; 0 ) ) ;
Int ( Round( Number * 100 ; Decimals ) ) &
Case ( ( Number * 100 ) = Int ( Number * 100 ) ; Left ( ".0000000000" ; Decimals + 1 ) ;
Round ( Number * 100 ; Decimals ) - Int( Round ( Number * 100 ; Decimals ) ) = 0 ; Left ( ".0000000000" ; Decimals + 1 ) ;
Left ( Round ( Number * 100 ; Decimals ) - Int( Round ( Number * 100 ; Decimals ) ) & "0000000000" ; Decimals+1 ) ) )
& "%"
 
Tom R.   Tom R., Denver
May 9, 2012
It would be cool if this could be modified to accept a minimum decimal and a maximum decimal parameter, where the minimum would be used in cases where the number is an integer and max is for a non-integer. E.g. I'd prefer to display "50%" or "50.00%" rather than "50.000000%", but if the numeber is non integer I would want to see 6 decimal places, such as "3.056214%".
 
Will Loving   Will Loving, Dedication Technologies, Inc.
May 9, 2012
Tom - To achieve the result you are looking for, simply replace the first "Result" text - Left ( ".0000000000" ; Decimals + 1 ) - in the second Case statement (3rd line of the calculation) with empty quotes.
 
Tom   Tom, Denver
May 9, 2012
Thanks, Will. I'll give it a try.
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Under construction. Email me your wish list for improvements.