# Number

**This page covers Number datatype tips & tricks.**

## Contents |

## How to combine two numbers to one and *vice versa*

To combine two numbers into one, use the next function

nCombined = VisNumberMakeLong( nValue1, nValue2)

You can find this function in **vtmisc.apl**

Beware that the two input parameters must have a value between 0 and 65535.

To get the two values back from the combined number

Set nValue1= SalNumberLow( nCombined ) Set nValue2= SalNumberHigh( nCombined )

## How to convert a number to string using prefixed zeros

An easy way to do that is using this function

sResult = SalFmtFormatNumber( nValue, sPicture )

So if you want to have a string with 5 characters and if nValue results in less characters and should be prefixed with zeros

Set nValue = 12 Set sResult = SalFmtFormatNumber( nValue, "00000" ) ! Picture parameter has 5 zeros specified ! sResult has the value "00012"

## An alternative way to define a number in boolean expressions

Look at the next piece of code

If bOk = TRUE Set nValue = 1 Else Set nValue = -1

It can be rewritten using one line of code using this function from **vtmisc.apl**

nResult = VisNumberChoose( bExpression, nTrueNumber, nFalseNumber )

So the If/Else construction above can be rewritten to

Set nValue = VisNumberChoose( bOk = TRUE, 1, -1 )

See also String : An alternative way to define a string in boolean expressions

## How to convert from one base to another (eg decimal to hexadecimal to binary)

Developers familiar with the C(++) language will probably know conversion functions like **itoa** and **strtol**.

The itoa function (integer to ascii) is able to convert/format a decimal integer number to a given base (eg hexadecimal, binary, octal etc).

The function strtol does the opposite, converting a base to decimal.

These functions are also available to other programming languages with the MSCRT library (C runtime).

So also for TD.

First declare these external functions (below the declarations for TD versions prior to TD5.x) :

Library name: MSVCRT.dll Function: _itoa Export Ordinal: 0 Returns Parameters Number: INT Receive String: LPSTR Number: INT Function: strtol Export Ordinal: 0 Returns Number: LONG Parameters String: LPCSTR Number: LPVOID Number: INT

Below the declarations for TD 5.x versions :

Library name: MSVCRT.dll Function: _itow Export Ordinal: 0 Returns String: LPWSTR Parameters Number: INT Receive String: LPWSTR Number: INT Function: wcstol Export Ordinal: 0 Returns Number: LONG Parameters String: LPCWSTR Number: LPVOID Number: INT

See more info concerning these CRT functions on MSDN :

**_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow**

**strtol, wcstol, _strtol_l, _wcstol_l**

The next piece of code converts a decimal integer to the given base

Call SalStrSetBufferLength( sBuffer, 32 ) ! Set nToBase to 2..36 (2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal) Call _itoa( nDecimalNumber, sBuffer, nToBase ) ! sBuffer holds the converted decimal in the given base

Here the code to convert a base number to decimal

Set nNumber = strtol( sBaseValue, NUMBER_Null, nBase )

Examples

! Next, decimal to hexadecimal Call _itoa( 125, sBuffer, 16 ) ! sBuffer = "7d" ! Next, decimal to octal Call _itoa( 125, sBuffer, 8 ) ! sBuffer = "175" ! Next, decimal to binary Call _itoa( 125, sBuffer, 2) ! sBuffer = "1111101" ! ! Next, binary to decimal Set nNumber = strtol( "101, NUMBER_Null, 2 ) ! nNumber = 5

Using these functions you are able to convert from one base to another.

The sample can be downloaded below, it uses wrapper PAL functions for the CRT functions and a Base2Base function.

(Separate TD5.x sample provided)

## Enter new tip title here

Enter new tip description here