Permalink
Browse files

Fix some numeric rounding issues in ruby 1.9.2

  • Loading branch information...
1 parent b94af29 commit 29cf7a684a940d21e0eb3b0d36992991c40510b7 @chriseppstein committed Sep 18, 2010
Showing with 16 additions and 8 deletions.
  1. +1 −1 lib/sass/script/color.rb
  2. +1 −7 lib/sass/script/number.rb
  3. +14 −0 lib/sass/util.rb
View
@@ -401,7 +401,7 @@ def smallest
end
def rgba_str
- "rgba(#{rgb.join(', ')}, #{alpha % 1 == 0.0 ? alpha.to_i : alpha})"
+ "rgba(#{rgb.join(', ')}, #{Sass::Util.tolerate(alpha)})"
end
def hex_str
@@ -35,12 +35,6 @@ class Number < Literal
# @return [Boolean, nil]
attr_accessor :original
- # The precision with which numbers will be printed to CSS files.
- # For example, if this is `1000.0`,
- # `3.1415926` will be printed as `3.142`.
- # @api public
- PRECISION = 1000.0
-
# @param value [Numeric] The value of the number
# @param numerator_units [Array<String>] See \{#numerator\_units}
# @param denominator_units [Array<String>] See \{#denominator\_units}
@@ -254,7 +248,7 @@ def inspect(opts = {})
elsif int?
self.value.to_i
else
- (self.value * PRECISION).round / PRECISION
+ Sass::Util.tolerate(self.value)
end
"#{value}#{unit_str}"
end
View
@@ -611,6 +611,20 @@ def set_eql?(set1, set2)
set1.to_a.uniq.sort_by {|e| e.hash}.eql?(set2.to_a.uniq.sort_by {|e| e.hash})
end
+ # The precision with which numbers will be printed to CSS files.
+ # For example, if this is `1000.0`,
+ # `3.1415926` will be printed as `3.142`.
+ # @api public
+ PRECISION = 1000.0
+
+ # Round to the nearest decimal point digit
+ # @param number - the number to tolerate
+ # @param precision - a multiple of 10
+ def tolerate(number, precision = PRECISION)
+ n = (number * precision).round / precision.to_f
+ n % 1 == 0 ? n.to_i : n
+ end
+
## Static Method Stuff
# The context in which the ERB for \{#def\_static\_method} will be run.

0 comments on commit 29cf7a6

Please sign in to comment.