Sprintf usage overview and Examples

Source: Internet
Author: User
Sprintf format

 

Sprintf format

The sprintf format of Ruby is basically the same as that of C language sprintf (3. But there are still some differences: it does not have modifiers for C-specific types, such as short or long; it contains 2-digit indicator (% B ); it does not support sprintf dialect syntax.

The sprintf format of Ruby is described in detail below.

The sprintf format specifications are as follows.[]Is optional.

 
% [Specify parameter $] [identifier] [width] [. Precision] indicator

To output'%'Here, please'%'Processing.

The following describes the usage of each element.

Identifier

The identifiers include '#', '+', '(Space),'-', and '0.

#

When a binary, octal, or hexadecimal indicator ('B', 'O', 'x', 'x') is used, the prefixes of "0b", "0", "0x", and "0x" are added respectively.

P sprintf ("% # B", 10) # => "0b1010"
P sprintf ("% # O", 10) # => "012"
P sprintf ("% # X", 10) # => "0xa"
P sprintf ("% # X", 10) # => "0xa"

For floating point numbers ('F', 'E', 'E', 'G', 'G'), "." must be added to the output ".".

P sprintf ("%. 0f", 10) # => "10"
P sprintf ("% #. 0f", 10) # => "10 ."
P sprintf ("%. 0e", 10) # => "1E + 01"
P sprintf ("% #. 0e", 10) # => "1.e+ 01"

In addition to the above features, 'G' and 'G' also add extra 0 at the end.

P sprintf ("%. 05g", 10) # => "10"
P sprintf ("% #. 05g", 10) # => "10.000"
+

The output string is signed. If it is a positive number, '+' is added '. It is only a numerical indicator ('D', 'I', 'B', 'O', 'x', 'x', 'U', 'F ', 'E', 'E', 'G', 'G. In addition, if it is 'B', 'O', 'x', 'x', or 'U', '-' is added to the negative number '-'.

P sprintf ("% d", 1) # => "1"
P sprintf ("% + D", 1) # => "+ 1"
P sprintf ("% x",-1) # => ".. f" # ".." indicates that f continues infinitely.
P sprintf ("% + X",-1) # => "-1"
''(Space)

Like '+', use a space to replace '+ '. It is only a numerical indicator ('D', 'I', 'B', 'O', 'x', 'x', 'U', 'F ', 'E', 'E', 'G', 'G.

P sprintf ("% d", 1) # => "1"
P sprintf ("% + D", 1) # => "+ 1"
P sprintf ("% d", 1) # => "1"
P sprintf ("% x",-1) # => ".. f"
P sprintf ("% x", 1) # => "1"
P sprintf ("% x",-1) # => "-1"
-

Back the output content to the left. If not specifiedWidthIt does not work.

0

When the output content is right-aligned, use '0' instead of a space to fill the excess part.

It is only a numerical indicator ('D', 'I', 'B', 'O', 'x', 'x', 'U', 'F ', 'G', 'G') (invalid for 'e', 'E)

P sprintf ("% 010d", 10)
# => "0000000010"

When used with '#', the output is as follows.

P sprintf ("% # 010x", 10) # => "0x0000000a"
P sprintf ("% # 010o", 10) # = & gt; "0000000012"
P sprintf ("% # 010b", 10) # => "0b00001010"

It is equivalent to the following example.

P sprintf ("% # 10.8x", 10) # => "0x0000000a"
P sprintf ("% # 10.9o", 10) # => "0000000012"
P sprintf ("% # 10.8b", 10) # => "0b00001010"

Normally, the following content is output.

P sprintf ("% # 10x", 10) # => "0xa"
P sprintf ("% # 10o", 10) # => "012"
P sprintf ("% # 10B", 10) # => "0b1010"
Width

A number string starting with a non-zero number is used to specify the width. Width refers to the width of the generated string, which is not subject toPrecision.

When the width is determinedIdentifier"," + ","-"," 0b "," 0 "," 0x "," 0x.

 
P sprintf ("% # 05x", 10) # => "0x00a"

Width refers to the "required minimum width". If the width of the result string exceeds the specified width, the specified width will be invalid.

If the width is specified as '*', the width value is obtained from the parameter.

 
P sprintf ("% 10 s", "foo") # => "foo"
P sprintf ("% * s", 10, "foo") # => "foo"
Precision

The number string followed by "." indicates the precision (if "." is used, it is ". 0 "). If an integer indicator ('D', 'I', 'B', 'O', 'x', 'U') is encountered, the precision indicates the length of the numeric part.

 
P sprintf ("% 10.5d", 1) # = & gt; "00001"
P sprintf ("% # 10.5x", 1) # => "0x00001"
P sprintf ("% + 10.5x", 1) # => "+ 00001"

If you encounter a floating point indicator ('F'), it indicates the number of digits in the decimal part.

 
P sprintf ("% 10.5f", 1) # => "1.00000"
P sprintf ("% 10.5f", 10) # => "10.00000"

If you encounter Floating Point Indicators ('E', 'E', 'G', 'G'), it indicates the number of valid digits.

 
P sprintf ("% 10.5e", 1) # => "1.00000e + 00"
P sprintf ("% 10.5e", 10) # => "1.00000e + 01"
P sprintf ("% 10.5g", 10) # => "10"
P sprintf ("% #10.5g", 10) # => "10.000"

If it is a string indicator ('s', 'P'), the length of the string in the parameter will be checked according to the precision, and the excess part will be removed. If the width and precision are set to the same value, only the precision-compliant part of the parameter string is output.

 
P sprintf ("% 10.2 s", "foo") # => "FO"
P sprintf ("% 5.5 s", "foo") #=>#=> "foo"
P sprintf ("% 5.5 s", "foobar") #=>#=> "fooba"

If the precision is set to '*', the precision value is extracted from the parameter.

 
P sprintf ("%. 5 s", "foobar") # => "fooba"
P sprintf ("%. * s", 5, "foobar") # => "fooba"
Indicator

It indicates the parameter type and is required. Generally speaking, it includes:

    • Indicator of the string: 'C', 's', 'P'
    • Indicator of integer: 'D', 'I', 'U', 'B', 'O', 'x ',
    • Indicator for floating point: 'F', 'G', 'E', 'E', 'G'

These types.

C

The parameter value (0x255) is considered as a character.CodeAnd output the corresponding characters. If the parameter is not a value, String, nil, true, or false, the to_int method will be used for conversion.

At this time, onlyIdentifier'-' And"Width"Is valid.

S

Output string.

If the parameter is not a string object, the to_s method is used to convert it.

P

Ruby 1.8 features: output the result of object # inspect.

P sprintf ("% s", [1, 2, 3]) # => "123"
P sprintf ("% P", [1, 2, 3]) # => "[1, 2, 3]"
D
I

The value in the parameter is output as a 10-digit integer.

If the parameter is not an integer, use the same rule as the integer function to convert it to an integer.

U

Consider the value of a parameter as an unsigned integer and output it as a 10-digit integer.

P sprintf ("% u",-1) # => "... 4294967295"

The above code will output P ".." + 0xffff_ffff.to_s.

Ruby 1.7 features: In version 1.7, "..." is not attached "..". If '% U' is used, the parameter is treated as a fixed-length integer. For negative integer N

Printf ("% u", n)

And

Printf ("% d", N &~ (-1 <n. Size * 8 ))

Is a meaning.

B
O
X
X

An integer is output in the form of a binary, octal, hexadecimal, or hexadecimal (uppercase) string.

If '#' is used, add "0b", "0", "0x", "0x" to the front ".

If the '+' and ''identifiers are not used,"... "is added before the negative number (if the '#' identifiers exist, after" 0x "and so on "..". This indicates that the maximum character is infinitely extended. It uses the complement form of 2 to express a negative number.

P sprintf ("% # B", 10) # => "0b1010"
P sprintf ("% # O", 10) # => "012"
P sprintf ("% # X", 10) # => "0xa"
# Add ".." to negative number ".."
P sprintf ("% # B",-1) # => "0b .. 1"
P sprintf ("% # O",-1) # => "0 .. 7"
P sprintf ("% # X",-1) # => "0x. F"
P sprintf ("% 10x",-1) # => ". F"
P sprintf ("%-10x",-1) # => ". F"
# If "precision" is specified, "..." is not added ".."
P sprintf ("%. 10x",-1) # => "ffffffffff"
F
E
E
G
G

'F' outputs numeric values in decimal form (XXX. XXX.

'E' outputs values in exponential form (X. xxxe + XX.

'G' is special. When the index is smaller than-4 or exceeds the precision range, it uses the 'E' Method for output. In addition, it uses the 'F' Method for output. In addition, it will delete the 0 at the end of the decimal part.

Uppercase letters ('E', 'G') change the letters in the output to uppercase.

P sprintf ("% F", 1.0) # => "1.000000"
P sprintf ("% E", 1.0) # => "1.000000e + 00"
P sprintf ("% G", 1.0) # => "1"
P sprintf ("% F", 10.1) # => "10.100000"
P sprintf ("% E", 10.1) # => "1.0366e + 01"
P sprintf ("% G", 10.1) # => "10.1"
P sprintf ("% G", 10 ** 6) # => "1E + 06"
P sprintf ("% G", 10 **-5) # => "1e-05"

The default value is 6.

If you encounter an infinite limit or Nan (not a number), the output is as follows.

P sprintf ("% F", 1.0/0) # => "inf"
P sprintf ("% F",-1.0/0) # => "-inf"
P sprintf ("% F", 0.0/0) # => "Nan"
P sprintf ("% E", 1.0/0) # => "inf"
P sprintf ("% E",-1.0/0) # => "-inf"
P sprintf ("% E", 0.0/0) # => "Nan"
Parameter

This part has the lowest utilization frequency, so it is placed at the end.

Nth $

indicates that the nth parameter is used for formatting.

 P sprintf ("% 1 $ D, % 1 $ X, % 1 $ O", 10) 
=> "10,, 12 "
P sprintf (" % 3 $ D, % 2 $ X, % 1 $ O ", 1, 2, 3)
=>" 3, 2, 1 "

You can also use it if you do not want to change the Parameter order but only want to change the format.

 case env ['lc _ time'] 
when/^ ja_jp/
FMt = "% 1 $ d Year % 2 $ D month % 3 $ D Day "
else
FMt =" % 2 $ 02d/% 03 $ 2D/% 1 $ 02d "
end
P sprintf (FMT, 1, 4, 22)
=> "04/22/01"

You can also insert "*", then, use parameters to set the values of " width " and " precision .

 P sprintf ("% 5.2f", 1) ;#=> "1.00" 
P sprintf ("% *. * F ", 5, 2, 1) ;#=>" 1.00 "
P sprintf (" % 1 $ * 2 $. * 3 $ F ", 1, 5, 2); # =>" 1.00

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.