FormatElectronicComponentValue
Back to ListDescription: Sortable electronic component value conversion
FileMaker Prototype:
Set Variable [$res; ACFU_FormatElectronicComponentValue( string_s)]
Category: UTILITY
Function source:
function FormatElectronicComponentValue ( string s )
functionID 3540;
double n;
double factor;
array string r;
s = substitute ( s, "µ", "u"); // µ Doesn't work in the regex
string regex = "^([0-9,.]+)([RpnumkKM])?(\d*)([FfHhΩom])?";
string ns, nf, nd, nu, f;
if (regex_match ( regex, s) ) then
r = regex_extract ( regex, s);
ns = getvalue(r[1],2); // Start number
nf = getvalue(r[1],3); // Factor letter
nd = getvalue(r[1],4); // any decimals
nu = getvalue(r[1],5); // any unit
print r[1];
// get the numeric factor based on the nf(factor) and nu(unit)
case
: (nf == "R" )
factor = 1;
: (nf == "p" )
factor = 10^-12;
: (nf == "n" )
factor = 10^-9;
: (nf == "u" || nf == "µ")
factor = 10^-6;
: (nf == "m" && nu=="H")
factor = 10^-3;
: (nf == "m" || nf=="M" )
factor = 10^6;
: (nf == "k" || nf == "K" )
factor = 1000;
default
factor = 1;
end case
ns = ns + "." + nd; // put together number and decimals
n = double ( ns ) * factor;
else
n = double ( s );
end if
return format ( "%30.15f", round(n,14));
end
The FormatElectronicComponentValue function converts typical electronic component value markings into a sortable string.
This function can be particularly useful in applications that include a bill of materials (BOM) for electronic devices, where sorting component values like resistors, capacitors, and inductors by their markings is necessary.
Markings for these components follow specific conventions:
- Resistors marked as "4K7" represent 4700 ohms and may also be written as "4.7K ohms."
- Capacitors marked as "4.7 uF" can also appear as "4u7," meaning 4.7 × 10⁻⁶ or 0.0000047 F.
- Capacitors marked as "100pF" represent 100 × 10⁻¹² or 0.0000000001 F.
Sorting such component values in a logical order becomes possible with this formatting function. It outputs a 30-character fixed-width string with 15 decimal places, accommodating both very large and very small values.
To use this function, assign its result to a sorting field in your layout, such as in a calculated field. This allows you to sort based on this field for a more logical order.
Examples
Set Variable [BOM::CompSort; ACFU_FormatElectronicComponentValue("4K7")]
=> " 4700.000000000000000"
Set Variable [BOM::CompSort; ACFU_FormatElectronicComponentValue("100nF")]
=> " 0.000000100000000"
Set Variable [BOM::CompSort; ACFU_FormatElectronicComponentValue("200M")]
=> " 200000000.000000000000000"
Set Variable [BOM::CompSort; ACFU_FormatElectronicComponentValue("12pF")]
=> " 0.000000000012000"
Set Variable [BOM::CompSort; ACFU_FormatElectronicComponentValue("4700uF")]
=> " 0.004700000000000"
Set Variable [BOM::CompSort; ACFU_FormatElectronicComponentValue("100µH")]
=> " 0.000100000000000"
How it Works
A regular expression groups the input into four parts:
- ns: the initial number
- nf: the factor letter
- nd: any decimals
- nu: the unit (to determine if its inductor or capacitor)
An case-end case structure assigns a multiplication factor based on these components.
Finally, the function converts the value to a fixed-format string, with 30 characters, 15 decimals, and right-justified space padding.
Use Case
You can read more about it in the Dynamic Portal Sorting demo ( Also available for download in the download area ):
