'Welcome to the Commodore Computer Wiki!'

DEC (decimal)

Keyword Token Version Category(s)
DEC $D1* 3.5, 7.0+ Function

*Token $D1 is used for COLLECT in version 4.0!

Syntax

DEC(string)

Where string is a character string literal, variable or expression.

Purpose

Convert a character string to numeric value.

Return Value

An integer from 0 to 65535.

Notes

string may contain only spaces and at most 4 hexadecimal characters (the numerals 0 to 9 and letters A to F).

Compared to VAL(), which converts decimal-based text into a numeric value, this function converts hexadecimal-based text. Unfortunately, that is where the similarity ends. While VAL() is very flexible, DEC() is very anal.

Similarities:

  • text to numeric
  • ignores all spaces
  • empty string = zero
Difference VAL() DEC ()
invalid character return valid characters halt with error
negative numbers ok halt with error
fractional values ok halt with error
scientific notion ok halt with error
minimum (absolute, approx.) 1e-38 0
maximum (absolute, approx.) 1e+38 6e+4

Although useful for BASIC users with no concept of assembly language, or the beginning assembly language programmer, it is complete crap otherwise. Obviously a ten minute hack by the designers.

Examples

PRINT DEC("  12 3")
 291

PRINT DEC("  12,3")
?ILLEGAL QUANTITY ERROR

PRINT DEC("  12.3")
?ILLEGAL QUANTITY ERROR

PRINT DEC(" -12.3")
?ILLEGAL QUANTITY ERROR

PRINT DEC("1BED")
 7149

PRINT DEC("BED")
 3053

PRINT DEC("")
 0

Compare with VAL()
Contrast with STR$(), HEX$()


This page is a member of Commodore BASIC