Skip to main content

7.5) Representing Numbers In Strings: Formatting


  • When we import or export numbers to text files or convert numbers to strings using sprintf, we need to specify the string format
  • The following table lists the available conversion characters
Value Type Conversion Details
signed integer %d or %i Base 10
unisgned integer %u Base 10
%o Base 8 (octal)
%x Base 16 (hexadecimal), lowercase letters a-f
%X Same as %x, uppercase letters A-F
floating point number %f Fixed point notation
%e Exponential notation, such as 3.141593e+00
%E Same as %e, but uppercase, such as 3.141593E+00
%g The more compact of %e or %f, with no trailing zeros
%G The more compact of %E or %f, with no trailing zeros
%bx or %bX
%bo
%bu
Double precision hexadecimal, octal or decimal value
Example: %bx prints pi as 400921fb54442d18
%tx or %tX
%to
%tu
Single precision hexadecimal, octal or decimal value
Example: %bx prints pi as 40490fdb
floating point number %c Single character
%s String of characters