XLISP *float-format*

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# *float-format*

Type: | - | system variable
Source: | - | xlprin.c

## Syntax

*float-format*
  returns - the print format for floating-point numbers

## Description

*float-format* is a system variable that allows a user to specify how floating point numbers are to be printed by XLISP. The value of *float-format* should be set to one of the string expressions "%e", "%f" or "%g". These format strings are similar to C-language floating point specifications:

`"%e"` | -`` | exponential. The number is converted to decimal notation of the form:
    [-]m.nnnnnnE[+-]xx
There is one leading digit. There are 6 digits after the decimal point.
`"%f"` | -`` | decimal. The number is converted to decimal notation of the form:
    [-]mmmmmm.nnnnnn
There are as many digits before the decimal point as necessary. There are 6 digits after the decimal point.
`"%g"` | -`` | shortest. The number is converted to either the form of "%e" or "%f", whichever produces the shortest output string. Non-significant zeroes are not printed.

The default value for *float-format* is the string "%g".

There are several additional flags and options available:

`` | `+` | `` -`` | always print the sign
`` | `space` | `` -`` | print a space instead of a + sign
`` | `#` | `` -`` | always print the dot, do not remove zeros after the dot
`` | `.n` | `` -`` | number of digits after the dot

The flags and options must be written between the "%" and the formatting letter, as shown in the examples below.

## Examples

    (setq *float-format* "%e")       ; exponential notation
    (print 1.0)                      => 1.000000e+00
    (print -9e99)                    => -9.000000e+99

    (setq *float-format* "%f")       ; decimal notation
    (print 1.0)                      => 1.000000
    (print 1.0e4)                    => 10000.000000
    (print -999.99e-99)              => -0.000000

    (setq *float-format* "%g")       ; shortest notation
    (print 1.0)                      => 1
    (print 1.0e7)                    => 1e+07
    (print -999.999e99)              => -9.99999e+101

    (setq *float-format* "%+g")      ; always print the sign
    (print  1.1)                     => +1.1
    (print -1.1)                     => -1.1

    (setq *float-format* "% g")      ; print a space instead of the + sign
    (print  1.1)                     =>  1.1
    (print -1.1)                     => -1.1

    (setq *float-format* "%#.10g")   ; ten digits after the dot
    (print 1.0)                      => 1.000000000
    (print 1.0e7)                    => 10000000.00
    (print -999.9999999e99)          => -9.999999999e+101

    (setq *float-format* "%+#.10g")  ; ten digits after the dot plus sign
    (print  1.2345)                  => +1.234500000
    (print -1.2345)                  => -1.234500000

    (setq *float-format* "%%")       ; bad format
    (print 1.0)                      => %%

    (setq *float-format* "%g")       ; reset to shortest notation

Note: The string in the *float-format* variable is the format specifier for the the underlying 'sprintf' C-function.

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference