Numbers

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

This topic deals with numbers in general, and how to convert numbers to strings. For mathematical operations with numbers see Mathematical operators.
Numbers could be integers (AKA whole numbers) – without decimal point – and real numbers (AKA floating point numbers). Since Just BASIC has no Boolean (logical, yes/no true/false) datatype, numbers are used for this purpose as well. Just BASIC has only one number type ("just a number"), so all this complexity is hidden.
If you want to understand what’s going on "under the hood", there is an article at LPBE describing how LB (and JB) stores numbers, what abilities it has, and what limitations and gotchas we can encounter: Numbers in Liberty BASIC.

Syntax

There is no syntax for using numbers. There are some conventions for how to represent numbers in JB. (I suppose you could call this syntax...).
  1. a whole number: 3.0; -2.0; 0.0
  2. an integer: 3; -2; 0
  3. a floating point: 3.14159; -2.333; 0.001
  4. scientific notation: 1.23e10; -2.3e-33
This is common shortcut for 1.23*10^10; -2.3*10^(-33), big E works as well.

Hints

Displaying numbers. Generally speaking, the result of a math operation involving whole numbers will be an integer number with no decimal point and 0. Operations involving decimal numbers will produce results as floating point numbers of arbitrary length.
Rounding. There is no direct way to truncate or round a number in JB, say to 2 places, except you can display a number to a rounded output using the using() function (see below). The integer function (int()) does not round a number; it merely returns the whole number to the left of the decimal point.
Converting numbers. There is no need to define a number type, or to use a function to convert to a different type. This is all done automatically in JB. You can, however, convert a number to a string using the STR$() function. This function returns the argument as a character representation of the number.
You cannot do mathematical calculations with a string representation of a number. To do the calculations, you must use the VAL() function to convert the string back to a number.
The Using() Function. The using function (See USING()), uses a template string (a mask), to represent the number of characters to display to each side of the decimal point. When doing this, the numbers to the right of the decimal point are automatically rounded and truncated to fit the template. Numbers to the left of the decimal are never truncated or rounded. If the number of digits is less than the characters in the template, leading spaces are prepended to pad the number to match the template. If the template shows fewer characters than there are digits in the number, the number is not truncated; instead, a percent sign (%) is prepended to the number display to show it does not fit the template. You should be aware of these conventions when displaying numbers in columns. Here's an example:
num=12345.67890
print "1", using("#####.##", num)
print "2", using("#######.##", num)
print "3", using("##.##", num)
'produces:
'1             12345.68
'2               12345.68
'3             %12345.68

'instead, do this:
print "1", using("#######.##", 12345.67890)
print "2", using("#######.##", 123456.67890)
print "3", using("#######.##", 12.67890)
'which produces:
'1               12345.68
'2              123456.68
'3                  12.68

Example

' Place a simple, working example of the keyword here

Useful Procedures

' Place a useful function or sub using this keyword here