xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

Val()

Convert a character string containing digits to numeric data type

Syntax

Val( <cNumber> ) --> nNumber

Arguments

<cNumber>
This is a character string containing digits. In addition, it may contain a decimal point and/or sign.

Return

The function returns a numeric value as the result of the conversion operation. If <cNumber> does not represent a number, the return value is zero.

Description

Val() analyses the parameter <cNumber> which may contain only "+-.0123456789". If <cNumber> contains any other character, a letter, for example, or if a second decimal point is detected, the conversion is terminated and the digits analysed so far are returned as numeric value. Leading blank space characters, however, are ignored.

The function recognizes the SET FIXED and SET DECIMALS settings and rounds the number if <cNumber> contains more decimal places than set with SET DECIMAL.

Info

See also:Round(), SET DECIMALS, SET FIXED, Str(), Transform()
Category: Character functions , Conversion functions
Source:rtl\val.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example shows return values of Val() depending on SET FIXED
// and SET DECIMALS

   PROCEDURE Main

      SET DECIMALS TO 2
      SET FIXED ON

      ? Val( "123.456" )                // result: 123.46
      ? Val("  123.456")                // result:   123.46
      ? Val( "123*456" )                // result:     123.00
      ? Val("x123*456" )                // result:        0.00

      ? Val( "-123.456" )               // result: -123.46
      ? Val( "  -123.456" )             // result:    -123.46
      ? Val( "-   123.456" )            // result:        0.00

      SET DECIMALS TO 3
      SET FIXED OFF

      ? Val( "123.456" )                // result: 123.456
      ? Val("  123.456")                // result:   123.456
      ? Val( "123*456" )                // result:     123
      ? Val("x123*456" )                // result:        0

      ? Val( "-123.456" )               // result: -123.456
      ? Val( "  -123.456" )             // result:    -123.456
      ? Val( "-   123.456" )            // result:        0.000

   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe