Format Specifiers in Julia - GeeksforGeeks

12 Oct, 2020
Format Specifiers are used to format the Input and Output of a Programming language. It tell the compiler about the data type of the Input that is being passed to it. Julia contains a package Printf and using its macro @printf takes input string value to be formatted and accepts parameters as variables to apply to the string.@printf is a macro in Julia, which has the following syntax:
@printf([io::IOstream], "%Fmt", args...)


It is used to print a space before the variable value over which it is specified. The variable value can belong to any data type, numeric, or strings. 
Julia
# declaring a variable string  val = "Space"  # importing moduleusing Printf  # adding space identifier after a string “Added” @printf("Added, % s \n", val)
Output:
Added, Space 

On specifying a numeric value as argument with the %+d format, ‘+’ sign is added before it. On specifying floating-point number as argument only the part before decimal is returned as output with +ve sign. 
Julia
# declaring numeric value num1 = 111  # importing required moduleusing Printf  # adding positive sign before numerical value@printf("Adding %+d", num1)  # declaring floating point numbernum2 = 10.1  # adding positive sign before floating number@printf("Adding positive sign %+d", num2)
Output:
+111
+11


The floating numbers are converted to integers with a positive sign.
The output returned can be explicitly left-justified by applying a minus sign after the symbol %. The following code, for example, % 16.4f justifies the passed parameter in a field of 16 characters with two decimal places. 
Julia
# importing moduleusing Printf  # declaring a floating point numbernum1 = 10.1234  # left justifying the number @printf("|%-16.4f|", num1)
Output:
|10.1234         |

This format specifier can be used to insert a % sign. There are no parameters specified explicitly. It can be used for both strings or numbers. 
Julia
# importing moduleusing Printf  # embedded in numerical values@printf("2 %% 8 = 0")  # embedding in strings @printf("hello %% gfg ")
Output:
2 % 8 = 0
hello % gfg

This representation is used to donate a hexadecimal number of the specified numeric value in either lower case or upper case characters %x in lower case and %X in upper case. Only numerical values can be specified as parameters in this argument list. 
Julia
# importing moduleusing Printf  # converting 100 to lower case hexadecimal@printf("% x", 100)  # converting 13 to lower case hexadecimal@printf("% x", 13)  # converting 234 to upper case hexadecimal@printf("% X", 234)
Output:
64
d
EA


This representation is used to denote the number specified as the argument in the octal form. The variable to be converted cannot be a floating-point number. 
Julia
# importing moduleusing Printf  # converting 372 from decimal # to octal representation@printf("% o", 372)  # converting 9 to octal@printf("% o", 9)
Output:
564
11


This format specifier is used to express the value in a scientific way with %E expressing the output in upper case characters and %e in lower case characters. 
Julia
using Printf  # converting numerical and floating numbers# scientific notation in lower case@printf("% e", 100)@printf("% e", 12.56)  # converting numerical and floating numbers#  scientific notation in upper case@printf("% E", 100)@printf("% E", 99.78)
Output:
1.000000e+02
1.256000e+01
1.000000E+02
9.978000E+01


Used to convert a number into floating-point integer values. Works both the numerical and decimal point numbers. It appends exactly 6 digits after the decimal, by default. We can specify the number of digits in %.xf parameter, where x indicates the number of digits.  
Julia
using Printf  # converting numerical to decimal point@printf("% f", 100)  # converting floating to decimal point@printf("% f", 12.56)  # printing decimal number upto 4 places only@printf("%.4f", 12.56)
Output:
100.000000
12.560000
12.5600


Julia Input/Output
Picked
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Get array dimensions and size of a dimension in Julia - size() Method
Searching in Array for a given element in Julia
Find maximum element along with its index in Julia - findmax() Method
Working with Excel Files in Julia
Getting the maximum value from a list in Julia - max() Method
Get number of elements of array in Julia - length() Method
Exception handling in Julia
Working with Date and Time in Julia
Getting the absolute value of a number in Julia - abs() Method
NamedTuple in Julia