HyperMath

Formatting

Formatting

Previous topic Next topic No expanding text in this topic  

Formatting

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

StrFormat(formatstring, e1, e2, ...)

or

string.format(formatstring, e1, e2, ...)

Creates a formatted string from the format and arguments provided.  This is similar to the printf("format",...) function in C.  An additional option %q puts quotes around a string.

string.format(formatstring, e1, e2, ...)

formatstring

The format specifying the string.  It can have format specification fields (see below).

e1, e2,…

The arguments to the format string.

 


A format specification, which consists of optional and required fields, has the following form starting with the escape character %:

% [width] [.precision]type

 

type

A required character field that specifies how the associated argument is to be interpreted, either as a character, string, or number.  It can be any of these:


d i

Decimal signed integer.


o

Octal integers


x X

Hex integer


u

Unsigned integer


c

Character


s

String


f

double


e E

double in exponential notation


g G

double normal or exponential notation, depending on magnitude


q

Quoted String

width

An optional integer that causes to pad the output with leading spaces so that it is at least the width number of characters wide.

precision

An optional integer.  For a non-integer numeric type, causes the decimal portion of the output to be expressed in at least precision digits.  For string type, causes the output to be truncated at precision characters.

> print(StrFormat("%s %q", "Hello", "HyperMath user!"))   // string and quoted string

  Hello "HyperMath user!"
> print(string.format("%e, %E", PI,PI))       // exponent
3.141593e+000, 3.141593E+000

  > print (StrFormat("%f, %g", PI,PI))       // float and compact float

  3.141593, 3.14159

  > print(StrFormat("%.2f", PI))// two decimal places

  3.14

  > print(StrFormat("%d, %i, %u", -100,-100,-100))    // signed, signed, unsigned integer

  -100, -100, 4294967196

  > print(StrFormat("%o, %x, %X", -100,-100,-100))    // octal, hex, hex

  37777777634, ffffff9c, FFFFFF9C

  > print(StrFormat("Chapter %u", 7))