File: number.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (101 lines) | stat: -rw-r--r-- 3,356 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module Rubyvis
  module Format
    class Number
      def initialize
        @mini = 0
        @maxi = Infinity # default maximum integer digits
        @mins = 0.0 # mini, including group separators
        @minf = 0.0 # default minimum fraction digits
        @maxf = 0.0 # default maximum fraction digits
        @maxk = 1 # 10^maxf
        @padi = "0" # default integer pad
        @padf = "0" # default fraction pad
        @padg = true # whether group separator affects integer padding
        @decimal = "." # default decimal separator
        @group = "," # default group separator
        @np = "\u2212" # default negative prefix
        @ns = "" # default negative suffix
      end
      
      
      def to_proc
        that=self
        lambda {|*args|  args[0] ? that.format(args[0]) : nil }
      end
      #/** @private */
      def format(x) 
        
        # /* Round the fractional part, and split on decimal separator. */
        x = (x * @maxk).round.quo(@maxk) if (Infinity > @maxf)
        x = (x.to_f-x.to_i==0) ? x.to_i : x.to_f
        s = x.abs.to_s.split(".")
        
        #/* Pad, truncate and group the integral part. */
        i = s[0]
        
        i = i[i.size-@maxi, i.size] if (i.size > @maxi) 
        if (@padg and (i.size < @mini))
          i = Array.new(@mini - i.size + 1).join(@padi) + i.to_s
        end
        
        if (i.size > 3)
          i = i.gsub(/\B(?=(?:\d{3})+(?!\d))/, @group)
        end
        
        if (@padg=="" and (i.size < @mins))
          i = Array.new(mins - i.size + 1).join(@padi) + i.to_s
        end
        
        s[0] = x < 0 ? @np + i + @ns : i
        
        #/* Pad the fractional part. */
        f = s[1].nil? ? "" : s[1]
        if (f.size < @minf)
          s[1] = f + Array.new(@minf - f.size + 1).join(@padf)
        end
        s.join(@decimal)
      end

      # Sets or gets the minimum and maximum number of fraction digits. The
      # controls the number of decimal digits to display after the decimal
      # separator for the fractional part of the number. 
      # If the number of digits is smaller than the minimum, the digits 
      # are padded; if the number of digits is
      # larger, the fractional part is rounded, showing only the higher-order
      # digits. The default range is [0, 0].
      #
      # If only one argument is specified to this method, this value is used as
      # both the minimum and maximum number. If no arguments are specified, a
      # two-element array is returned containing the minimum and the maximum.
      #
      # @function
      # @name Rubyvis.Format.number.prototype.fractionDigits
      # @param {number} [min] the minimum fraction digits.
      # @param {number} [max] the maximum fraction digits.
      # @returns {Rubyvis.Format.number} <tt>this</tt>, or the current fraction digits.
      
      def fraction_digits(min=nil,max=nil)
        if (!min.nil?)
          max||=min
          @minf = min
          @maxf = max
          @maxk = 10**@maxf
          return self
        end
        [@minf, @maxf]
      end
      
      
      def integer_digits(min=nil,max=nil)
        if (!min.nil?)
          max||=min
          @mini=min
          @maxi=max
          @mins=@mini+(@mini/3.0).floor*@group.size 
          return self
        end
        [@mini, @maxi]
      end
    end
  end
end