Ruby精度を取る


精度を取って、私たちは使いました
eval(sprintf("%.3f",flaot_params))

実際の状況.
require 'benchmark'

times = 1000000
float = 9.234234765765765764
Benchmark.bm do |r|
  r.report('SPf  :') {
    times.times do
      f1 = sprintf('%.2f' ,float).to_f
    end
  }
  r.report('*100 :') {
    times.times do
      f2 = (100 * float).round/100.0
    end
  }
  r.report('*.01 :') {
    times.times do
      f3 = (100 * float).round*0.01
    end
  }
end

#        user     system      total        real
#SPf  :  5.590000   0.050000   5.640000 (  5.720373)
#*100 :  3.760000   0.040000   3.800000 (  3.829771)
#*.01 :  1.770000   0.010000   1.780000 (  1.811917)

roundとsprintfのAPIの説明:
参照
rand(max=0) => number
Converts max to an integer using max1 = max.to_i.abs. If the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1. Kernel::srand may be used to ensure repeatable sequences of random numbers between different runs of the program. Ruby currently uses a modified Mersenne Twister with a period of 2**19937-1.
関連するものもあります
number_with_precision(50/3,5)
#=>16.00000

number_with_precision(50.0/3,5)
#=>16.66667 

Trick:Railsのnumber_with_precision
参照
  format(format_string [, arguments...] ) => string
sprintf(format_string [, arguments...] ) => string
Returns the string resulting from applying format_string to any additional arguments. Within the format string, any characters other than format sequences are copied to the result. A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding sprintf argument is to be interpreted, while the flags modify that interpretation. The field type characters are listed in the table at the end of this section. The flag characters are:
  Flag     | Applies to   | Meaning
  ---------+--------------+-----------------------------------------
  space    | bdeEfgGiouxX | Leave a space at the start of
           |              | positive numbers.
  ---------+--------------+-----------------------------------------
  (digit)$ | all          | Specifies the absolute argument number
           |              | for this field. Absolute and relative
           |              | argument numbers cannot be mixed in a
           |              | sprintf string.
  ---------+--------------+-----------------------------------------
   #       | beEfgGoxX    | Use an alternative format. For the
           |              | conversions `o', `x', `X', and `b',
           |              | prefix the result with ``0'', ``0x'', ``0X'',
           |              |  and ``0b'', respectively. For `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.
  ---------+--------------+-----------------------------------------
  +        | bdeEfgGiouxX | Add a leading plus sign to positive numbers.
  ---------+--------------+-----------------------------------------
  -        | all          | Left-justify the result of this conversion.
  ---------+--------------+-----------------------------------------
  0 (zero) | bdeEfgGiouxX | Pad with zeros, not spaces.
  ---------+--------------+-----------------------------------------
  *        | 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.
The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field. For numeric fields, the precision controls the number of decimal places displayed. For string fields, the precision determines the maximum number of characters to be copied from the string. (Thus, the format sequence %10.10s will always contribute exactly ten characters to the result.)
The field types are:
    Field |  Conversion
    ------+--------------------------------------------------------------
      b   | Convert argument as a binary number.
      c   | Argument is the numeric code for a single character.
      d   | Convert argument as a decimal number.
      E   | Equivalent to `e', but uses an uppercase E to indicate
          | the exponent.
      e   | Convert floating point argument into exponential notation
          | with one digit before the decimal point. The precision
          | determines the number of fractional digits (defaulting to six).
      f   | Convert floating point argument as [-]ddd.ddd,
          |  where the precision determines the number of digits after
          | the decimal point.
      G   | Equivalent to `g', but use an uppercase `E' in exponent form.
      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 d.dddd form otherwise.
      i   | Identical to `d'.
      o   | Convert argument as an octal number.
      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.
      u   | Treat argument as an unsigned decimal number. Negative integers
          | are displayed as a 32 bit two's complement plus one for the
          | underlying architecture; that is, 2 ** 32 + n.  However, since
          | Ruby has no inherent limit on bits used to represent the
          | integer, this value is preceded by two dots (..) in order to
          | indicate a infinite number of leading sign bits.
      X   | Convert argument as a hexadecimal number using uppercase
          | letters. Negative numbers will be displayed with two
          | leading periods (representing an infinite string of
          | leading 'FF's.
      x   | Convert argument as a hexadecimal number.
          | Negative numbers will be displayed with two
          | leading periods (representing an infinite string of
          | leading 'ff's.
Examples:
   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)                        #=> "..4294967173"