PRINTING WITHOUT SPACES

It may not be immediately apparent to the new Forth programmer how to print without spaces between items.

Examples

In this example the programmer wants the constant “ref1” to be printed with the character “V” immediately following, ie “10V”. The common Forth print statement is used in Voltage1 Example Code below.

.” Hello”  ( - - ) Compiles a string and prints it when executed.

Voltage1 Example Code

10 constant ref1

: voltage1 ( -- )  ref1 . ." V" cr ;

Voltage1 Output

voltage1  10 V

As you can see there is a space between the “10” and the “V” which is not what was wanted. The behaviour of this print method is the correct and desired behaviour, so what do we do ?

The Solution

The solution is to use Pictured Numerical Output which can format a text string however one desires.

Voltage2 Example Code

10 constant ref1

: voltage2 ( -- )  ref1    0 <# #s #> type  ." V"  cr ;

Voltage2 Output

voltage2  10V

Explanation

The word #S converts the value on the stack into ASCII characters. It will only produce as many digits as are necessary to represent the number; it will not produce leading zeroes. But it always produces at least one digit, which will be zero if the value was zero.