本文簡要介紹ruby語言中 Kernel.format
的用法。
用法
format(format_string [, arguments...] ) → string
別名:sprintf
返回將 format_string
應用於任何其他參數所產生的字符串。在格式字符串中,格式序列以外的任何字符都將複製到結果中。
格式序列的語法如下。
%[flags][width][.precision]type
格式序列由百分號、後跟可選標誌、寬度和精度指示符組成,然後以字段類型字符結束。字段類型控製如何解釋相應的sprintf
參數,而標誌修改該解釋。
字段類型字符為:
Field | Integer Format ------+-------------------------------------------------------------- b | Convert argument as a binary number. | Negative numbers will be displayed as a two's complement | prefixed with `..1'. B | Equivalent to `b', but uses an uppercase 0B for prefix | in the alternative format by #. d | Convert argument as a decimal number. i | Identical to `d'. o | Convert argument as an octal number. | Negative numbers will be displayed as a two's complement | prefixed with `..7'. u | Identical to `d'. x | Convert argument as a hexadecimal number. | Negative numbers will be displayed as a two's complement | prefixed with `..f' (representing an infinite string of | leading 'ff's). X | Equivalent to `x', but uses uppercase letters. Field | Float Format ------+-------------------------------------------------------------- e | Convert floating point argument into exponential notation | with one digit before the decimal point as [-]d.dddddde[+-]dd. | The precision specifies the number of digits after the decimal | point (defaulting to six). E | Equivalent to `e', but uses an uppercase E to indicate | the exponent. f | Convert floating point argument as [-]ddd.dddddd, | where the precision specifies the number of digits after | the decimal point. g | Convert a floating point number using exponential form | if the exponent is less than -4 or greater than or | equal to the precision, or in dd.dddd form otherwise. | The precision specifies the number of significant digits. G | Equivalent to `g', but use an uppercase `E' in exponent form. a | Convert floating point argument as [-]0xh.hhhhp[+-]dd, | which is consisted from optional sign, "0x", fraction part | as hexadecimal, "p", and exponential part as decimal. A | Equivalent to `a', but use uppercase `X' and `P'. Field | Other Format ------+-------------------------------------------------------------- c | Argument is the numeric code for a single character or | a single character string itself. p | The valuing of argument.inspect. s | Argument is a string to be substituted. If the format | sequence contains a precision, at most that many characters | will be copied. % | A percent sign itself will be displayed. No argument taken.
標誌修改格式的行為。標誌字符是:
Flag | Applies to | Meaning ---------+---------------+----------------------------------------- space | bBdiouxX | Leave a space at the start of | aAeEfgG | non-negative numbers. | (numeric fmt) | For `o', `x', `X', `b' and `B', use | | a minus sign with absolute value for | | negative values. ---------+---------------+----------------------------------------- (digit)$ | all | Specifies the absolute argument number | | for this field. Absolute and relative | | argument numbers cannot be mixed in a | | sprintf string. ---------+---------------+----------------------------------------- # | bBoxX | Use an alternative format. | aAeEfgG | For the conversions `o', increase the precision | | until the first digit will be `0' if | | it is not formatted as complements. | | For the conversions `x', `X', `b' and `B' | | on non-zero, prefix the result with ``0x'', | | ``0X'', ``0b'' and ``0B'', respectively. | | For `a', `A', `e', `E', `f', `g', and 'G', | | force a decimal point to be added, | | even if no digits follow. | | For `g' and 'G', do not remove trailing zeros. ---------+---------------+----------------------------------------- + | bBdiouxX | Add a leading plus sign to non-negative | aAeEfgG | numbers. | (numeric fmt) | For `o', `x', `X', `b' and `B', use | | a minus sign with absolute value for | | negative values. ---------+---------------+----------------------------------------- - | all | Left-justify the result of this conversion. ---------+---------------+----------------------------------------- 0 (zero) | bBdiouxX | Pad with zeros, not spaces. | aAeEfgG | For `o', `x', `X', `b' and `B', radix-1 | (numeric fmt) | is used for negative numbers formatted as | | complements. ---------+---------------+----------------------------------------- * | all | Use the next argument as the field width. | | If negative, left-justify the result. If the | | asterisk is followed by a number and a dollar | | sign, use the indicated argument as the width.
標誌示例:
# `+' and space flag specifies the sign of non-negative numbers.
sprintf("%d", 123) #=> "123"
sprintf("%+d", 123) #=> "+123"
sprintf("% d", 123) #=> " 123"
# `#' flag for `o' increases number of digits to show `0'.
# `+' and space flag changes format of negative numbers.
sprintf("%o", 123) #=> "173"
sprintf("%#o", 123) #=> "0173"
sprintf("%+o", -123) #=> "-173"
sprintf("%o", -123) #=> "..7605"
sprintf("%#o", -123) #=> "..7605"
# `#' flag for `x' add a prefix `0x' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%x", 123) #=> "7b"
sprintf("%#x", 123) #=> "0x7b"
sprintf("%+x", -123) #=> "-7b"
sprintf("%x", -123) #=> "..f85"
sprintf("%#x", -123) #=> "0x..f85"
sprintf("%#x", 0) #=> "0"
# `#' for `X' uses the prefix `0X'.
sprintf("%X", 123) #=> "7B"
sprintf("%#X", 123) #=> "0X7B"
# `#' flag for `b' add a prefix `0b' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%b", 123) #=> "1111011"
sprintf("%#b", 123) #=> "0b1111011"
sprintf("%+b", -123) #=> "-1111011"
sprintf("%b", -123) #=> "..10000101"
sprintf("%#b", -123) #=> "0b..10000101"
sprintf("%#b", 0) #=> "0"
# `#' for `B' uses the prefix `0B'.
sprintf("%B", 123) #=> "1111011"
sprintf("%#B", 123) #=> "0B1111011"
# `#' for `e' forces to show the decimal point.
sprintf("%.0e", 1) #=> "1e+00"
sprintf("%#.0e", 1) #=> "1.e+00"
# `#' for `f' forces to show the decimal point.
sprintf("%.0f", 1234) #=> "1234"
sprintf("%#.0f", 1234) #=> "1234."
# `#' for `g' forces to show the decimal point.
# It also disables stripping lowest zeros.
sprintf("%g", 123.4) #=> "123.4"
sprintf("%#g", 123.4) #=> "123.400"
sprintf("%g", 123456) #=> "123456"
sprintf("%#g", 123456) #=> "123456."
字段寬度是一個可選的整數,後跟一個可選的句點和一個精度。寬度指定將寫入該字段結果的最小字符數。
寬度示例:
# padding is done by spaces, width=20
# 0 or radix-1. <------------------>
sprintf("%20d", 123) #=> " 123"
sprintf("%+20d", 123) #=> " +123"
sprintf("%020d", 123) #=> "00000000000000000123"
sprintf("%+020d", 123) #=> "+0000000000000000123"
sprintf("% 020d", 123) #=> " 0000000000000000123"
sprintf("%-20d", 123) #=> "123 "
sprintf("%-+20d", 123) #=> "+123 "
sprintf("%- 20d", 123) #=> " 123 "
sprintf("%020x", -123) #=> "..ffffffffffffffff85"
對於數字字段,精度控製顯示的小數位數。對於字符串字段,精度確定要從字符串中複製的最大字符數。 (因此,格式序列%10.10s
將始終為結果貢獻十個字符。)
精度示例:
# precision for `d', 'o', 'x' and 'b' is
# minimum number of digits <------>
sprintf("%20.8d", 123) #=> " 00000123"
sprintf("%20.8o", 123) #=> " 00000173"
sprintf("%20.8x", 123) #=> " 0000007b"
sprintf("%20.8b", 123) #=> " 01111011"
sprintf("%20.8d", -123) #=> " -00000123"
sprintf("%20.8o", -123) #=> " ..777605"
sprintf("%20.8x", -123) #=> " ..ffff85"
sprintf("%20.8b", -11) #=> " ..110101"
# "0x" and "0b" for `#x' and `#b' is not counted for
# precision but "0" for `#o' is counted. <------>
sprintf("%#20.8d", 123) #=> " 00000123"
sprintf("%#20.8o", 123) #=> " 00000173"
sprintf("%#20.8x", 123) #=> " 0x0000007b"
sprintf("%#20.8b", 123) #=> " 0b01111011"
sprintf("%#20.8d", -123) #=> " -00000123"
sprintf("%#20.8o", -123) #=> " ..777605"
sprintf("%#20.8x", -123) #=> " 0x..ffff85"
sprintf("%#20.8b", -11) #=> " 0b..110101"
# precision for `e' is number of
# digits after the decimal point <------>
sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
# precision for `f' is number of
# digits after the decimal point <------>
sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
# precision for `g' is number of
# significant digits <------->
sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
# <------->
sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
# precision for `s' is
# maximum number of characters <------>
sprintf("%20.8s", "string test") #=> " string t"
例子:
sprintf("%d %04x", 123, 123) #=> "123 007b"
sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
sprintf("%u", -123) #=> "-123"
對於更複雜的格式,Ruby 支持按名稱引用。 %<name>s 樣式使用格式樣式,但 %{name} 樣式不使用。
例子:
sprintf("%<foo>d : %<bar>f", { :foo => 1, :bar => 2 })
#=> 1 : 2.000000
sprintf("%{foo}f", { :foo => 1 })
# => "1f"
相關用法
- Ruby Kernel.frozen?用法及代碼示例
- Ruby Kernel.fail用法及代碼示例
- Ruby Kernel.local_variables用法及代碼示例
- Ruby Kernel.Integer用法及代碼示例
- Ruby Kernel.binding用法及代碼示例
- Ruby Kernel.`cmd`用法及代碼示例
- Ruby Kernel.autoload用法及代碼示例
- Ruby Kernel.loop用法及代碼示例
- Ruby Kernel.Hash用法及代碼示例
- Ruby Kernel.caller用法及代碼示例
- Ruby Kernel.set_trace_func用法及代碼示例
- Ruby Kernel.exit!用法及代碼示例
- Ruby Kernel.trap用法及代碼示例
- Ruby Kernel.String用法及代碼示例
- Ruby Kernel.select用法及代碼示例
- Ruby Kernel.syscall用法及代碼示例
- Ruby Kernel.then用法及代碼示例
- Ruby Kernel.sprintf用法及代碼示例
- Ruby Kernel.Pathname用法及代碼示例
- Ruby Kernel.srand用法及代碼示例
- Ruby Kernel.yield_self用法及代碼示例
- Ruby Kernel.BigDecimal用法及代碼示例
- Ruby Kernel.raise用法及代碼示例
- Ruby Kernel.test用法及代碼示例
- Ruby Kernel.pretty_inspect用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Kernel.format。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。