Aller au contenu principal
Version: Suivant

Num

Num ( expression {; separator} ) : Real
Num ( expression ; base ) : Real

ParamètresTypeDescription
expressionText, Boolean, IntegerString for which to return the numeric form, or Boolean to return 0 or 1, or Numeric expression
separatorTextDecimal separator
baseIntegerValue between 2 and 36 that represents the radix
RésultatRealNumeric form of the expression parameter
Historique
ReleaseModifications
21Prise en charge du paramètre base

Description

The Num command returns the numeric form of the String, Boolean or numeric expression you pass in expression.

When expression is of the string type, you can use a separator parameter or a base parameter (see below).

Expressions de type Chaîne

When you use the Num command with a string expression, two syntaxes are available:

  • Num(string{;separator})
  • Num(string;base)

Num(string{;separator})

If expression consists only of one or more alphabetic characters, Num returns a zero. If expression includes alphabetic and numeric characters, the command ignores the alphabetic characters. Thus, it transforms the string "a1b2c3" into the number 123.

There are three reserved characters that Num treats specially: the decimal separator as defined in the system (if the separator parameter is not passed), the hyphen “-”, and “eor “E”. These characters are interpreted as numeric format characters.

  • The decimal separator is interpreted as a decimal place and must appear embedded in a numeric string. By default, the command uses the decimal separator set by the operating system. You can modify this character using the separator parameter (see below).
  • The hyphen causes the number or exponent to be negative. The hyphen must appear before any negative numeric characters or after the “e” for an exponent. Except for the “e” character, if a hyphen is embedded in a numeric string, the portion of the string after the hyphen is ignored. For example, Num("123-456") returns 123, but Num("-9") returns -9.
  • The e or E causes any numeric characters to its right to be interpreted as the power of an exponent. The “e” must be embedded in a numeric string. Thus, Num("123e–2") returns 1.23.
    Note that when the string includes more than one "e", conversion might give different results under macOS and under Windows.

separator parameter

The separator parameter designates a custom decimal separator for evaluating the expression. When the string to be evaluated is expressed with a decimal separator different from the system operator, the command returns an incorrect result. The separator parameter can be used in this case to obtain a correct evaluation. When this parameter is passed, the command does not take the system decimal separator into account. You can pass one or more characters.

note

The GET SYSTEM FORMAT command can be used to find out the current decimal separator as well as several other regional system parameters.

Num(string;base)

Using a base parameter (integer) triggers a specific mode in which you specify the radix (base) of the number expressed as string in expression. In particular, this syntax allows you to convert hexadecimal strings to numbers.

Dans le paramètre base, passez la base dans laquelle est exprimée le nombre dans expression. Vous pouvez passer n'importe quelle valeur entière comprise entre 2 et 36.

Si vous passez 0 dans base, la commande détermine la base en fonction de la valeur de expression. Si expression commence par "0x", la base 16 est utilisée. Sinon, la base 10 est utilisée.

If expression evaluates to a decimal number, only the integer part is converted.

info
  • This syntax strictly follows the parseInt EcmaScript specification.
  • L'utilisation de cette syntaxe avec base=10 ne donnera pas exactement les mêmes résultats que l'utilisation de la syntaxe sans le paramètre base. For example, in compliance with the EcmaScript specification, any character that does not belong to the base is considered a separator (see examples).

Expressions de type Booléen

If you pass a Boolean expression, Num returns 1 if the expression is True; otherwise, it returns 0 (zero).

Expressions numériques

If you pass a numeric expression in the expression parameter, Num returns the value passed in the expression parameter as is. This can be useful more particularly in the case of generic programming using pointers.

Undefined Expressions

If expression evaluates to undefined, the command returns 0 (zero). This is useful when you expect the result of an expression (e.g. an object attribute) to be a number, even if it can be undefined.

Exemple 1

The following example illustrates how Num works when passed a single string argument:

$result:=Num("ABCD") // 0
$result:=Num("A1B2C3") // 123
$result:=Num("123") // 123
$result:=Num("123.4") // 123.4
$result:=Num("–123") // –123
$result:=Num("–123e2") // –12300

Exemple 2

Here, [Client]Debt is compared with $1000. The Num command applied to these comparisons returns 1 or 0. Multiplying 1 or 0 with a string repeats the string once or returns the empty string. As a result, [Client]Risk gets either “Good” or “Bad”:

  // If client owes less than 1000, a good risk.
  // If client owes more than 1000, a bad risk.
 [Client]Risk:=("Good"*Num([Client]Debt<1000))+("Bad"*Num([Client]Debt>=1000))

Exemple 3

This example compares the results obtained depending on the “current” separator:

 $thestring:="33,333.33"
 $thenum:=Num($thestring)
  // by default, $thenum equals 33,33333 on a French system
 $thenum:=Num($thestring;".")
  // $thenum will be correctly evaluated regardless of the system;
  // for example, 33 333,33 on a French system

Exemple 4

This example illustrates the use of the base syntax:

$result:=Num("ff";16) // 255 (lower-case hexadecimal)
$result:=Num("0xFF") // 0
$result:=Num("0xFF";16) // 255
$result:=Num("2";2) // 0
$result:=Num("10.3";16) // 16
$result:=Num("123.20") // 12320 (standard base 10 syntax)
$result:=Num("123.20"; 10) // 123 (explicitly specify base 10)

Voir également

Bool
GET SYSTEM FORMAT
String

Propriétés

Numéro de commande11
Thread safe