From: Stefano Crocco Date: 2008-01-06T04:29:21+09:00 Subject: Re: Convert bignum to numeric and formatting --Boundary-00=_9w9fHqG6QFMBDIZ Content-Type: Multipart/Mixed; boundary="Boundary-00=_9w9fHqG6QFMBDIZ" --Boundary-00=_9w9fHqG6QFMBDIZ Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Alle sabato 5 gennaio 2008, Damaris Fuentes ha scritto: > Ok, this works with the "require" statement (I've put it in > environment.rb) > > Well, however, this does not work as it was expected. > I've made an script as this: > ***** > require 'extensions/numeric' > > n = 10067064106.format_s(:eu, :sep => '.') > puts n > ****** > and the output is 0.067.064.106, without the first "1". > > Why is it deleting the first digit? Is Numeric valid just for numbers of > 11 digits? > > Lots of thanks. It's a bug of extensions (it works correctly using another separator). The problem is that in extensions/numeric.rb there are calls to gsub! like: dec.gsub!(/#{sm}$/, '') where sm is the separator. Since the dot is a special character in a regexp, it doesn't work as intended. It should have been: dec.gsub!(/#{Regexp.quote(sm)}$/, '') I attach a working copy of extensions/numeric you can use instead of the original one (just replace the file. If you used rubygems installed, it should be somewhere like RUBY_INSTALL_PATH/gems/1.8/gems/extensions/lib/numeric) I'd also report a bug at rubyforge, but I'm not sure whether the project is still active (there are no news past 2004, no messages in the forums and no bugs). Does anyone know more about this Stefano --Boundary-00=_9w9fHqG6QFMBDIZ Content-Type: application/x-ruby; name="numeric.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="numeric.rb" #!/usr/local/bin/ruby -w # # =3D=3D extensions/integer.rb # # Adds methods to the builtin Numeric and Integer classes. # require "extensions/_base" # # * Integer#even? # ExtensionsProject.implement(Integer, :even?) do class Integer # # Returns true if this integer is even, false otherwise. # 14.even? # -> true # 15.even? # -> false # def even? self % 2 =3D=3D 0 end end end # # * Integer#odd? # ExtensionsProject.implement(Integer, :odd?) do class Integer # # Returns true if this integer is odd, false otherwise. # -99.odd? # -> true # -98.odd? # -> false # def odd? self % 2 =3D=3D 1 end end end # # This code arose from discussions with Francis Hwang. Leaving it here for= future work. # # class Numeric # def precision_format(nplaces, flag =3D :pad) # format =3D "%.#{nplaces}f" # result =3D sprintf(format, self) # case flag # when :pad # when :nopad # result.sub!(/\.?0*$/, '') # else # raise ArgumentError, "Invalid value for flag: #{flag.inspect}" # end # result # end # end # # 100.precision_format(2) # -> "100.00" # 100.precision_format(2, :nopad) # -> "100" # 100.1.precision_format(2) # -> "100.10" # 100.1.precision_format(2, :nopad) # -> "100.1" # 100.1.precision_format(2, false) # # -> "ArgumentError: Invalid value for flag: false" # ExtensionsProject.implement(Numeric, :format_s) do #-- # Copyright =A9 2003 Austin Ziegler # # Permission is hereby granted, free of charge, to any person obtaining a= copy # of this software and associated documentation files (the "Software"), to # deal in the Software without restriction, including without limitation = the # rights to use, copy, modify, merge, publish, distribute, sublicense, an= d/or # sell copies of the Software, and to permit persons to whom the Software= is # furnished to do so, subject to the following conditions: #=20 # The above copyright notice and this permission notice shall be included= in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL= THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEA= LINGS # IN THE SOFTWARE. #++ class Numeric # # Provides the base formatting styles for #format_s. See #format_s for # more details. Two keys provided that are not supported in the # #format_s arguments are: # # :style:: Allows a style to inherit from other styles. Styles # will be applied in oldest-first order in the event # of multiple inheritance layers. # :id:: This must be provided on any default style created # or provided so as to provide a stop marker so that # recursive styles do not result in an infinite loop. # # This is an implementation detail, not important for users of the clas= s. # FORMAT_STYLES =3D { :us =3D> { :sep =3D> ',', :dec =3D> '.', :id =3D> :us }, :usd =3D> { :style =3D> :us, :currency =3D> { :id =3D> "$", :pos= =3D> :before }, :id =3D> :usd }, :eu =3D> { :sep =3D> ' ', :dec =3D> ',', :id =3D> :us }, :euro =3D> { :style =3D> :eu, :currency =3D> { :id =3D> "=80", :p= os =3D> :before }, :id =3D> :euro }, :percent =3D> { :style =3D> :us, :currency =3D> { :id =3D> "%%", :po= s =3D> :after }, :id =3D> :percent } } # # Format a number as a string, using US or European conventions, and # allowing for the accounting format of representing negative numbers. # Optionally, currency formatting options can be provided. # # For example: # x =3D -10259.8937 # x.format_s # =3D> "-10,259.8937" # x.format_s(:us) # =3D> "-10,259.8937"=20 # x.format_s(:usd) # =3D> "$-10,259.8937" # x.format_s(:eu) # =3D> "-10 259,8937"=20 # x.format_s(:euro) # =3D> "=80-10 259,8937" # x.format_s(:us, :acct =3D> true) # =3D> "(10,259.8937)" # x.format_s(:eu, :acct =3D> true) # =3D> "(10 259,8937)" # x.format_s(:usd, :acct =3D> true) # =3D> "$(10,259.8937)" # x.format_s(:euro, :acct =3D> true) # =3D> "=80(10 259,8937)" # x.format_s(:percent) # =3D> "-10,259.8937%" # # You may configure several aspects of the formatting by providing keyw= ord # arguments after the country and accounting arguments. One example of = that # is the :acct keyword. A more insane example is: # # x =3D -10259.8937 # x.format_s(:us, # :sep =3D> ' ', :dec =3D> ',', # :neg =3D> '<%s>', :size =3D> 2,=20 # :fd =3D> true) # -> "<1 02 59,89 37>" #=20 # The keyword parameters are as follows: # # :acct:: If +true+, then use accounting style for negati= ve # numbers. This overrides any value for # :neg. # :sep:: Default "," for US, " " for Euro. Separate the # number groups from each other with this string. # :dec:: Default "." for US, "," for Euro. Separate the # number's integer part from the fractional part # with this string. # :neg:: Default "-%s". The format string used = to # represent negative numbers. If :acct is # +true+, this is set to "(%s)". # :size:: The number of digits per group. Defaults to # thousands (3). # :fd:: Indicates whether the decimal portion of the # number should be formatted the same way as the # integer portion of the number. ("fd" =3D=3D "fo= rmat # decimal".) Defaults to +false+. # :currency:: This is an optional hash with two keys, # :id and :pos. :id is # the string value of the currency (e.g., # "$", "=80", "USD "); # :pos is either :before or # :after, referring to the position of t= he # currency indicator. The default :pos is # :before. # def format_s(style =3D :us, configs=3D{}) style =3D FORMAT_STYLES[style].dup # Adopt US style by default. # Deal with recursive styles. if style[:style] styles =3D [] s =3D style while s[:style] s =3D FORMAT_STYLES[s[:style]].dup styles << s break if s[:style] =3D s[:id] end styles.reverse_each { |s| style.merge!(s) } end # Merge the configured style. style.merge!(configs) sm =3D style[:sep] || ',' dp =3D style[:dec] || '.' if style[:acct] fmt =3D '(%s)' else fmt =3D style[:neg] || '-%s' end sz =3D style[:size] || 3 format_decimal =3D style[:fd] ng =3D (self < 0) fmt =3D "%s" if not ng dec, frac =3D self.abs.to_s.split(/\./) dec.reverse! dec.gsub!(/\d{#{sz}}/) { |m| "#{m}#{sm}" } dec.gsub!(/#{Regexp.quote(sm)}$/, '') dec.reverse! if format_decimal and not frac.nil? frac.gsub!(/\d{#{sz}}/) { |m| "#{m}#{sm}" } frac.gsub!(/#{Regexp.quote(sm)}$/, '') end if frac.nil? val =3D dec else val =3D "#{dec}#{dp}#{frac}" end if style[:currency] if style[:currency][:pos].nil? or style[:currency][:pos] =3D=3D :be= fore fmt =3D "#{style[:currency][:id]}#{fmt}" elsif style[:currency][:pos] =3D=3D :after fmt =3D "#{fmt}#{style[:currency][:id]}" end end fmt % val end end # class Numeric end # ExtensionsProject.implement --Boundary-00=_9w9fHqG6QFMBDIZ-- --Boundary-00=_9w9fHqG6QFMBDIZ--