clean num, clean if num
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.
Vectorization: This function supports vectorization in the 1st function parameter.
Instead of providing a single value, you can provide a set or even a nested set which contain multiple values.
The function will then process every value and its return value contains a corresponding set containing all results.
Indirect parameter passing is disabled
Vectorization is allowed in the 1st function parameter
1-2
No. | Type | Description |
---|---|---|
1 input |
numeral or string set |
Value If string and containing digits: Conversion to numeral. |
2 input |
string | Decimal symbol Use single character symbol, e.g. '.' or ','. |
Type | Description |
---|---|
numeral string |
number Contains resulting number. Strings may be returned by the function clean if num if the input value contains no digits. |
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
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