Pictured numeric output

To get numbers output in a particular format, FlashForth provides the basic words #, <#, #s, #>, sign and hold. These words are intended for use within word definitions. Here is an example of their use.
: (d.2) ( d -- caddr u) 
  tuck dabs <# # # [char] . hold #s rot sign #> ;  ok<#,ram>
A double number sits on the stack, with it most significant bits, including its sign, in TOS. First, tuck copies TOS (with the sign bit) to below the double number and then the absolute value is converted to a string of characters representing the unsigned value. Starting with the least significant digits, there will be two to the right of a decimal point. The phrase [char] . hold adds the decimal point to the string. In this phrase, [char] . builds in the representation of the decimal point as a numeric literal (ASCII code 46) and hold then adds it to the string under construction. After adding the decimal point, the word #s converts as many remaining digits as required. The word rot is used to bring the copy of the original most-significant cell to TOS and the word sign adds a negative sign to the string if required. Finally, word #> finishes the conversion, leaving the character-address of the resultant string and the number of characters in it on the top of the stack.
437658. (d.2) type 4376.58 ok<#,ram>
-437699. (d.2) type -4376.99 ok<#,ram>
45. (d.2) type 0.45 ok<#,ram>
Note that, with FlashForth, double integers must be entered as literal values with the decimal point as the last character.


Peter Jacobs 2013-06-12