Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Commit

Permalink
Fix bugs with fractional negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
zackbloom committed Nov 21, 2013
1 parent 1a47d72 commit acbb802
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
16 changes: 13 additions & 3 deletions odometer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ round = (val, precision=0) ->
val = Math.floor(val)
val /= Math.pow(10, precision)

truncate = (val) ->
# | 0 fails on numbers greater than 2^32
if val < 0
Math.ceil(val)
else
Math.floor(val)

fractionalPart = (val) ->
val - round(val)

Expand Down Expand Up @@ -386,7 +393,7 @@ class Odometer
# This assumes the value has already been rounded to
# @format.precision places
#
parser = /^\d*\.(\d*?)0*$/
parser = /^\-?\d*\.(\d*?)0*$/
for value, i in values
values[i] = value.toString()

Expand Down Expand Up @@ -425,8 +432,8 @@ class Odometer
# We create a array to represent the series of digits which should be
# animated in each column
for i in [0...digitCount]
start = Math.floor(oldValue / Math.pow(10, (digitCount - i - 1)))
end = Math.floor(newValue / Math.pow(10, (digitCount - i - 1)))
start = truncate(oldValue / Math.pow(10, (digitCount - i - 1)))
end = truncate(newValue / Math.pow(10, (digitCount - i - 1)))

dist = end - start

Expand Down Expand Up @@ -479,6 +486,9 @@ class Odometer
if j == 0
addClass numEl, 'odometer-first-value'

if start < 0
@addDigit '-'

mark = @inside.querySelector('.odometer-radix-mark')
mark.parent.removeChild(mark) if mark?

Expand Down
19 changes: 15 additions & 4 deletions odometer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit acbb802

Please sign in to comment.