clean num, clean if num

Prev Next

Function Names

clean num, clean if num

Description

This function converts a string into numeral, including eliminating redundant contents in commercial number representations such as currency symbols before or after themand other form of units. Thousand separators will be removed and decimal commas will be converted to decimal points. Minus signs both before and after the numerals will be detected. Numbers in parentheses, e.g. (100.00) = -100 are considered negative numbers. Scientific notation (e.g. 1.2E+03 ) is supported here. Numerals passed as parameters into thes function will be passed through. If the 1st parameter contains no number (e.g. letters, blanks) , then clean num returns 0 and clean if num returns the original string.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1-2

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)

Return value

TypeDescription
numeral
string
number

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

Examples

      echo( clean num ( "EUR 1'234.50" ), " and ", clean if num ( "EUR 1'234.50" ) ); // Both 1234.5
      echo( clean num ( "2,500 € " ),     " and ", clean num ( "2,500 €", ",") ); //  2500 and 2.5
      echo( clean num ( "2,500- € " ) ); // -2500
      echo( clean num ( "-   2,500" ),    ", ",    clean num ( "(2,500)" ) ); //  Both -2500
      echo( clean num ( "[2,500]" ),      " and ", clean num ( "-[2,500]" ) ); // 2500 (brackets alone do not negate)
      echo( clean num ( "(EUR 1,000)" ),  " and ", clean num ( "(EUR) 1,000") ); // Negation if number INSIDE parentheses
      echo( clean num ( "INR 30 40 500" ) ); //  3040500
      echo( clean num ( ".123" ),         " and ", clean num ( ".123", "," )); // returns 0.123 and 123.
      echo( clean num ( "" ),             " and '",clean if num( "" ), "'" ); // returns 0 and "";
      echo( clean num ( "abc" ),          " and ", clean if num ( "abc" ) ); //  returns 0 and abc

      echo( new line, "Scientific Notations:");
      echo( clean num ( 1.2E3 ), " and  ", clean num ( '1.2E+03' ) ); //  return 1200
      echo( clean num ( "1 2 E - 0 3" ) ); // returns 0.012

Output

1234.5 and 1234.5
2500 and 2.5
-2500
-2500, -2500
2500 and -2500
-1000 and 1000
3040500
0.123 and 123
0 and ''
0 and abc

Scientific Notations:
1200 and  1200
0.012
Try it yourself: Open LIB_Function_clean_num.b4p in B4P_Examples.zip. Decompress before use.

See also

numeral conversion function
smart num
smart if num