Sha256: 8476bacdccbd90b1b2933c234f89aee16353150160076d63791afec85af47352

Contents?: true

Size: 1.81 KB

Versions: 6

Compression:

Stored size: 1.81 KB

Contents

#--
# Credits::
# Copyright:: Tyler Rick
# License::   Ruby License
# Submit to Facets?:: Maybe
# Developer notes::
#++

require 'rubygems'
require 'facets/numeric/round'
require 'quality_extensions/module/alias_method_chain'

# * http://www.oreillynet.com/ruby/blog/2005/12/adding_utility_to_core_classes_1.html
#class Float
#  def round_to(x = nil)
#    if x > 0
#      ("%.0#{x.to_i}f" % self).to_f
#    else
#      # http://www.oreillynet.com/ruby/blog/2005/12/adding_utility_to_core_classes_1.html
#      # An advantage (I think) of Jonas' solution is it allows rounding on the other side of the decimal when x is negative.
#      # 1234.567.prec(-2) #=>1200.0
#      puts (self * 10**x).to_i
#      puts (10**x).to_f
#      (self * 10**x).round / (10**x)
#    end
#  end
#end

# File lib/core/facets/float/round.rb, line 17
# def round_at( d ) #d=0
#   (self * (10.0 ** d)).round.to_f / (10.0 ** d)
# end

class Float
  def truncate_with_precision(d)
   (self * (10.0 ** d)).truncate_without_precision.to_f / (10.0 ** d)
  end
  alias_method_chain :truncate, :precision
end 


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test
require 'test/unit'

class TheTest < Test::Unit::TestCase
  def test_0
    assert_equal 1234.0, 1234.567.truncate(0)
    assert_equal 1235.0, 1234.567.round_at(0)
  end

  def test_1
    assert_equal 1234.5, 1234.567.truncate(1)
    assert_equal 1234.6, 1234.567.round_at(1)
  end

  def test_2
    assert_equal 1234.56, 1234.567.truncate(2)
    assert_equal 1234.57, 1234.567.round_at(2)
  end

  def test_negative_1
    assert_equal 1230.0, 1235.567.truncate(-1)
    assert_equal 1240.0, 1235.567.round_at(-1)
  end

  def test_negative_2
    assert_equal 1200.0, 1254.567.truncate(-2)
    assert_equal 1300.0, 1254.567.round_at(-2)
  end
end
=end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
quality_extensions-1.1.6 lib/quality_extensions/float/truncate.rb
quality_extensions-1.1.0 lib/quality_extensions/float/truncate.rb
quality_extensions-1.1.4 lib/quality_extensions/float/truncate.rb
quality_extensions-1.1.2 lib/quality_extensions/float/truncate.rb
quality_extensions-1.1.3 lib/quality_extensions/float/truncate.rb
quality_extensions-1.1.1 lib/quality_extensions/float/truncate.rb