Class: FormatR::NumberFormatter
Overview
this format doesn’t care if it’s a @ or an ^, it acts the same and doesn’t chop things used for @##.## formats
Instance Method Summary collapse
- #formatInt(s) ⇒ Object
-
#formatString(s, unused_var_name = nil, unused_aBinding = nil) ⇒ Object
given a string that’s a number spit it back with the right number of digits and rounded the correct amount.
-
#getFract(num, spaceLeft) ⇒ Object
what portion of the number is after the decimal point and should be printed.
-
#initialize(wholeString, radix, fractionString) ⇒ NumberFormatter
constructor
A new instance of NumberFormatter.
Methods inherited from Formatter
Constructor Details
#initialize(wholeString, radix, fractionString) ⇒ NumberFormatter
Returns a new instance of NumberFormatter.
538 539 540 541 542 543 |
# File 'lib/formatr.rb', line 538 def initialize (wholeString, radix, fractionString) @whole = wholeString.size + 1 # for the '@' @fraction = fractionString.size @radix = radix.size #should always be 1 @len = @whole + @fraction + @radix end |
Instance Method Details
#formatInt(s) ⇒ Object
571 572 573 |
# File 'lib/formatr.rb', line 571 def formatInt (s) s.to_s.ljust(@len) end |
#formatString(s, unused_var_name = nil, unused_aBinding = nil) ⇒ Object
given a string that’s a number spit it back with the right number of digits and rounded the correct amount.
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/formatr.rb', line 547 def formatString (s, unused_var_name=nil, unused_aBinding=nil) if (s.size == 1) return formatInt(s) end num = s.split('.') # should this take into account internationalization? res = num[0] res = "" if (res.nil?) ## pgr xxx spaceLeft = @fraction + @radix if (res.size > @whole) spaceLeft = @len - res.size() end if (spaceLeft > 0) res += '.' spaceLeft -= 1 end res += getFract(num, spaceLeft) if (spaceLeft > 0) max = @len if (res.size > max) res = res[0,max] end res.rjust(max) end |
#getFract(num, spaceLeft) ⇒ Object
what portion of the number is after the decimal point and should be printed
576 577 578 579 580 581 582 583 584 585 586 |
# File 'lib/formatr.rb', line 576 def getFract (num, spaceLeft) num[1] = "" if (num[1].nil?) @fraction.times {num[1] += '0'} fract = num[1][0,spaceLeft + 1] if (fract.size() >= spaceLeft + 1) if ((fract[spaceLeft,1].to_i) >= 5 ) fract[spaceLeft - 1, 1] = ((fract[spaceLeft - 1, 1].to_i) + 1).to_s end end return fract[0,spaceLeft] end |