Skip to content

Commit

Permalink
Added a partial implementation of the excel TEXT(number,format) function
Browse files Browse the repository at this point in the history
  • Loading branch information
tamc committed Aug 30, 2011
1 parent ab0f6ef commit a4c1aec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/formulae/compile/formula_builder.rb
Expand Up @@ -177,6 +177,7 @@ def self.excel_function(name,name_to_use_in_ruby = name)
excel_function :pmt
excel_function :npv
excel_function :countif
excel_function :text

def standard_function(name_to_use_in_ruby,args)
"#{name_to_use_in_ruby}(#{args.map {|a| a.visit(self) }.join(',')})"
Expand Down
7 changes: 7 additions & 0 deletions lib/formulae/run/excel_functions.rb
Expand Up @@ -104,6 +104,13 @@ def find_or_create_worksheet(worksheet_name)
worksheet
end

def text(number,format)
return number if iserr(number)
return format if iserr(format)
raise Exception.new("format #{format} not implemented") unless format.is_a?(Numeric)
number.round(format).to_s
end

def round(number,decimal_places)
return number if iserr(number)
return decimal_places if iserr(decimal_places)
Expand Down
8 changes: 8 additions & 0 deletions spec/excel_functions_spec.rb
Expand Up @@ -316,6 +316,14 @@ def a1; 10.0; end
end
end

describe "text" do
it "should round a numeric value to a number of decimal places and then return a string if in the format text(3.1415,0), otherwise not implemented" do
FunctionTest.text(3.1415,0).should == "3"
FunctionTest.text(3.1415,1).should == "3.1"
end
end


describe "excel comparisons" do

it "should carry out comparisons in the usual way" do
Expand Down

0 comments on commit a4c1aec

Please sign in to comment.