lib/svgcode/gcode/command.rb in svgcode-0.3.0 vs lib/svgcode/gcode/command.rb in svgcode-0.4.0
- old
+ new
@@ -1,12 +1,15 @@
module Svgcode
module GCode
class Command
- INT_FORMAT = "%02d"
- FLOAT_FORMAT = "%.3f"
INT_LETTERS = [ 'M', 'G' ]
+ INT_ROUND_TO = 0
+ INT_FORMAT = "%02d"
+ FLOAT_ROUND_TO = 3
+ FLOAT_FORMAT = "%.#{FLOAT_ROUND_TO}f"
+
attr_reader :letter, :number, :args
def initialize(letter_or_str = nil, number = nil, args = [])
if letter_or_str.length > 1
parts = letter_or_str.split(/\s+/)
@@ -24,10 +27,12 @@
@number = @number.to_f unless @number.nil?
end
def to_s
num_fmt = INT_LETTERS.include?(@letter) ? INT_FORMAT : FLOAT_FORMAT
- str = "#{@letter}#{num_fmt % @number}"
+ round_to = num_fmt == INT_FORMAT ? INT_ROUND_TO : FLOAT_ROUND_TO
+
+ str = "#{@letter}#{num_fmt % @number.round(round_to)}"
str += " #{@args.join(' ')}" unless @args.nil? || @args.empty?
str
end
def ==(other)