best type

Prev Next

Function Names

best type

Description

In case the input parameter is a string, then it will be converted to the best type, depending on the contents. All non-string types (numerals, parameter sets, dates, booleans voids) will be passed through without modifications Following rules apply for strings provided as parameters

  • If the contents look like number, e.g. "123.45", then a conversion to numerals takes plae.
  • If the contents equals true or false, then a converion to boolean takes place.
  • Followng rules apply if unabiguous dates and/or times are provided:
    • "YYYY-MM-DD": Dates
    • "hh:mm:ss": Times
    • "YYYY-MM-DD hh:mm:ss": Dates with times
    • "0000-00-00" Blank dates
    • Any other characters, including spaces before or after dates, will suppress date conversion.
    • If the 2nd parameter is true, then strings will be converted to parameters sets if the contents look like parameter sets, e.g. contents inside braces { ... }. The elements must be separated with commas.
  • In all other cases, the string contents are passed through without changes.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

1-2

Parameters

No.TypeDescription
1
input
valid types Value

Contents to be converted to best type.

2
input
boolean Do parameter sets

If 'true', then string contents looking like parameter set descriptions will be converted to parameter sets

Default value: false

Return value

TypeDescription
parameter set Result

Value converted into best type

Examples

  values[] =
     { 1.2, date(today), true, { 1,2,{3,a} }, "Hello",  // No conversions
       "true", "True",         // Only 'true' will be converted
       "false", "False",       // Only 'false' will be converted
       "2020-04-17", "2020-04-07 22:30:00", "2020-04-07 22:30", // Time conversion if fully specified.  3rd exmaple si not converted
       "{1,2,{3,A} }",         // Converts to parameter set
       "1.2", "1E+3",          // Converts to numbers
       "0000-00-00" };         // Converts to blank date

  for all parameters( values[], v[] )
  {
      n[] = best type( v[], true ); // 2nd parameter set also asks to convert strings containing parameter sets to parameter sets.
      echo("Value: ", n[],  "   Type: ", type(n[]) );
  }

Output

Value: 1.2   Type: numeral
Value: 2024-07-14   Type: date
Value: true   Type: boolean
Value: {1,2,{3,'a'}}   Type: parameter set
Value: Hello   Type: string
Value: true   Type: boolean
Value: True   Type: string
Value: false   Type: boolean
Value: False   Type: string
Value: 2020-04-17   Type: date
Value: 2020-04-07 22:30:00   Type: date
Value: 2020-04-07 22:30   Type: string
Value: {1,2,{3,'A'}}   Type: parameter set
Value: 1.2   Type: numeral
Value: 1E+3   Type: numeral
Value:    Type: date
Try it yourself: Open LIB_Function_best_type.b4p in B4P_Examples.zip. Decompress before use.