Aleksandr Levchuk
Aleksandr Levchuk

Reputation: 3881

How do I round a float to a specified number of significant digits in Ruby?

It would be nice to have an equivalent of R's signif function in Ruby.

For example:

>> (11.11).signif(1)
10
>> (22.22).signif(2)
22
>> (3.333).signif(2)
3.3
>> (4.4).signif(3)
4.4 # It's usually 4.40 but that's OK. R does not print the trailing 0's
    # because it returns the float data type. For Ruby we want the same.
>> (5.55).signif(2)
5.6

Upvotes: 13

Views: 8270

Answers (8)

moger777
moger777

Reputation: 1326

I think you can do this with the built in to_d method (https://rubyapi.org/3.3/o/float#method-i-to_d). It converts it to a decimal but you can convert back to a float:

require 'bigdecimal'
require 'bigdecimal/util'

11.11.to_d(1).to_f # 10.0
22.22.to_d(2).to_f # 22.0
3.333.to_d(2).to_f # 3.3
4.4.to_d(3).to_f # 4.4
5.55.to_d(2).to_f # 5.6

Upvotes: 2

Bravoking
Bravoking

Reputation: 399

Here's an implementation that doesn't use strings or other libraries.

class Float
  def signif(digits)
    return 0 if self.zero?
    self.round(-(Math.log10(self).ceil - digits))
  end
end

Upvotes: 8

habudu
habudu

Reputation: 303

@Blou91's answer is nearly there, but it returns a string, instead of a float. This below works for me:

(sprintf "%.2f", 1.23456).to_f

So as a function,

def round(val, sig_figs)
  (sprintf "%.#{sig_figs}f", val).to_f
end

Upvotes: 0

dgasper
dgasper

Reputation: 212

You are probably looking for Ruby's Decimal.

You could then write:

require 'decimal/shortcut'
num = 1.23541764
D.context.precision = 2
num_with_2_significant_digits = +D(num.to_s) #  => Decimal('1.2')
num_with_2_significant_digits.to_f #  => 1.2000000000000002

Or if you prefer to use the same syntax add this as a function to class Float like this:

class Float
  def signif num_digits
    require 'decimal/shortcut'
    D.context.precision = num_digits
    (+D(self.to_s)).to_f
  end
end

Usage would then be the same, i.e.

 (1.23333).signif 3
 # => 1.23

To use it, install the gem

gem install ruby-decimal

Upvotes: 3

radhika
radhika

Reputation: 534

Some of the previous answers and comments have alluded to this solution but this is what worked for me:

# takes in a float value and returns another float value rounded to 
# given significant figures.    
def round_to_sig_figs(val, sig_figs)
  BigDecimal.new(val, sig_figs).to_f
end

Upvotes: 3

Blou91
Blou91

Reputation: 101

Use sprintf if you want to print trailing zeros

2.0.0-p353 :001 > sprintf "%.3f", 500
 => "500.000"
2.0.0-p353 :002 > sprintf "%.4f", 500
 => "500.0000"
2.0.0-p353 :003 >

Upvotes: -2

Victor Deryagin
Victor Deryagin

Reputation: 12225

There is probably better way, but this seems to work fine:

class Float
  def signif(signs)
    Float("%.#{signs}g" % self)
  end
end

(1.123).signif(2)                    # => 1.1
(11.23).signif(2)                    # => 11.0
(11.23).signif(1)                    # => 10.0

Upvotes: 16

mu is too short
mu is too short

Reputation: 434665

I don't see anything like that in Float. Float is mostly a wrapper for the native double type and given the usual binary/decimal issues, I'm not that surprised that Float doesn't allow you to manipulate the significant digits.

However, BigDecimal in the standard library does understand significant digits but again, I don't see anything that allows you to directly alter the significant digits in a BigDecimal: you can ask for it but you can't change it. But, you can kludge around that by using a no-op version of the mult or add methods:

require 'bigdecimal'
a = BigDecimal.new('11.2384')
a.mult(1, 2) # the result is 0.11E2   (i.e. 11)
a.add(0, 4)  # the result is 0.1124E2 (i.e. 11.24)

The second argument to these methods:

If specified and less than the number of significant digits of the result, the result is rounded to that number of digits, according to BigDecimal.mode.

Using BigDecimal will be slower but it might be your only choice if you need fine grained control or if you need to avoid the usual floating point problems.

Upvotes: 3

Related Questions