Skip to content

Commit

Permalink
A better way to determine if an RK record is sufficient, or if we nee…
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes Wyss authored and hannes-wyss committed Jul 3, 2009
1 parent 283d023 commit 618221d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/spreadsheet/excel/writer/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,19 @@ def name
unicode_string @worksheet.name
end
def need_number? cell
(cell.is_a?(Numeric) && cell.abs > 0x1fffffff) \
|| (cell.is_a?(Float) \
&& !/^[\000\001]\000{3}/.match([cell * 100].pack(EIGHT_BYTE_DOUBLE)))
if cell.is_a?(Numeric) && cell.abs > 0x1fffffff
true
elsif cell.is_a?(Float)
higher = cell * 100
if higher == higher.to_i
need_number? higher.to_i
else
test1, test2 = [cell * 100].pack(EIGHT_BYTE_DOUBLE).unpack('V2')
test1 > 0 || need_number?(test2)
end
else
false
end
end
def row_blocks
# All cells in an Excel document are divided into blocks of 32 consecutive
Expand Down
1 change: 1 addition & 0 deletions test/excel/writer/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_need_number
assert_equal false, sheet.need_number?(0.1)
assert_equal false, sheet.need_number?(0.01)
assert_equal true, sheet.need_number?(0.001)
assert_equal true, sheet.need_number?(10000000.0)
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions test/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1185,5 +1185,29 @@ def test_bignum
assert_equal smallnum - 0.1, book.worksheet(0)[0,3]
assert_equal(-smallnum - 0.1, book.worksheet(0)[1,3])
end
def test_bigfloat
# reported in http://rubyforge.org/tracker/index.php?func=detail&aid=24119&group_id=678&atid=2677
bigfloat = 10000000.0
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
sheet[0,0] = bigfloat
sheet[0,1] = bigfloat + 0.1
sheet[0,2] = bigfloat - 0.1
sheet[1,0] = -bigfloat
sheet[1,1] = -bigfloat + 0.1
sheet[1,2] = -bigfloat - 0.1
path = File.join @var, 'test_big-float.xls'
book.write path
assert_nothing_raised do
book = Spreadsheet.open path
end
sheet = book.worksheet(0)
assert_equal bigfloat, sheet[0,0]
assert_equal bigfloat + 0.1, sheet[0,1]
assert_equal bigfloat - 0.1, sheet[0,2]
assert_equal -bigfloat, sheet[1,0]
assert_equal -bigfloat + 0.1, sheet[1,1]
assert_equal -bigfloat - 0.1, sheet[1,2]
end
end
end

0 comments on commit 618221d

Please sign in to comment.