smart num, smart if num

Prev Next

Function Names

smart num, smart if num

Description

These two functions are an upgrade variant of clean num() and clean if num(). The functions smart num and smart if num provide automatic up- and downscaling feature if (e.g. metric) scaling units are specified. As long no user specific units and scaling factors are specified in the 3rd and 4th parameters, the following units will be recognized and rescale the numbers. These stanard units are not case sensitive.

General Scaling Factors:

Units Scaling Factor Explanation
¢, pct, % 0.01 Cent, percent
‰ (‰) 0.001 Per mille, part per thousand, see 1
‱ (&ertenk;), bp 0.000 1 Part per ten thousand, base point (in finance), see 1
pcm 0.000 01 Percent mille, part per hundred thousand
ppm 0.000 001 Part per million
ppb 0.000 000 001 Part per billion
k, t, tsd 1,000 Kilo, thousand
m, mn, mln, mio 1,000,000 Mega, million
g, b, bn, bln 1,000,000,000 Giga, billion (Note: B4P uses the U.S. English interpretation of billion)
mrd, md, mia 1,000,000,000 Milliard (common in Great Britain and other languages)

Indian Scaling Factors:

Units Scaling Factor Explanation
lakh, lk, lac 100,000 Indian hundred thousand
crore, cr 10,000,000 Indian ten milllion
arab 1,000,000,000 Indian 1 billion (British English: Indian 1 milliard)

Prefix letters before 3-letter currency codes, not case sensitive;

Txxx, Kxxx 1,000 TGBP, KUSD, KEUR, etc.
Mxxx 1,000,000 MUSD, MCHF
Bxxx 1,000,000 BUSD, BRUB

The different prefixes can be combined, e.g. smart num ( 1 kEUR % ) returns 10.

The function smart if num behaves similarly like clean if num: Contents without numbers inside are passed through as strings.

Application hint: After loading a table from a different source, apply this function on all applicable number columns in order to clean them up and do necessary scalings.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1, 2, 4

Parameters

No.TypeDescription
1
input
numeral or string Value

If string and containing digits: Conversion to numeral.
If string and containing no digits or string is blank: Conversion to 0 (clean num) or string returned (function clean if num) If numeral: Will be passed through.

2
input
string Decimal symbol

Use single character symbol, e.g. '.' or ','.
Specify 'local' to use currently applicable locale settings. Examples: It chooses '.' for countries like USA, United Kingdom and Switzerland, and ',' for Germany and Austria.

Default value: . (point)
Opt. 3
input
parameter set containing strings Custom units

Specify at least 1 unit name. The units provided will be used instead (and not in addition) of the standard units. Attention:Comparison of these units is case-sensitive. This allows to distinguish lower-case letters like "m" for milli and upper-case leeters like "M" for mega.
Attention: 4th parameter must also be specified.

Default value: Standard units apply (not case-sensitive)
Opt. 4
input
parameter set containing numerals Custom scaling factors

For each custom unit defined in the previous function parameter, the correponding scaling factor needs to be added.

Return value

TypeDescription
numeral
string
number

Contains resulting number. Strings may be returned by the function smart if num if the input value contains no digits.

Examples

      echo( smart num( "5.0 MUSD" )); // 5,000,000
      echo( smart num( "EUR 1.2mn" )); // 1,200,000
      echo( smart num( "INR 0.5 lk" )); // 50,000
      echo( smart num( '50%' )); // 0.5
      echo( smart num( 'Alc. 0.5 ‰' )); // 0.0005
      echo( smart num( "1.23cr" ));  // 12,300,000
      echo( smart num( 1.23m, ".", { m, M }, {0.000001, 1000000} ));

Output

5000000
1200000
50000
0.5
0.00005
12300000
0.00000123
Try it yourself: Open LIB_Function_smart_num.b4p in B4P_Examples.zip. Decompress before use.

See also

numeral conversion function
clean num
clean if num