Project

General

Profile

Feature #16011

Digit grouping

Added by svnpenn (Steven Penny) about 1 year ago. Updated about 1 year ago.

Status:
Rejected
Priority:
Normal
Target version:
-
[ruby-core:93839]

Description

Ruby seems to have no way to format a number with grouped thousands. I see Rails
has an option:

require 'active_support/all'
1234.to_s(:delimited)

However in this case it seems that grouping cannot be combined with say, leading
zeros:

https://github.com/rails/rails/issues/36707

This is quite simple with other languages, for example JavaScript:

(1234).toLocaleString(0, {minimumIntegerDigits: 7});
"0,001,234"

Python:

>>> format(1234, '08,')
'0,001,234'

Go:

package main
import "golang.org/x/text/language"
import "golang.org/x/text/message"
func main() {
   message.NewPrinter(language.English).Printf("%07d\n", 1234)
   // 0,001,234
}

Related issues

Related to Ruby master - Feature #12447: Integer#digits for extracting digits of place-value notation in any baseClosedmatz (Yukihiro Matsumoto)Actions
#1

Updated by znz (Kazuhiro NISHIYAMA) about 1 year ago

  • Backport deleted (2.5: UNKNOWN, 2.6: UNKNOWN)
  • Tracker changed from Bug to Feature

Updated by duerst (Martin Dürst) about 1 year ago

Just for the record:

  • What to use as thousands separator is locale-dependent. It can be a comma (US), a dot (Germany), an apostrophe (Switzerland), a space (Sweden; usually a narrow-width, non-breaking space), and so on.
  • Some locales don't use thousands separators, and the separators don't have to come in regular intervals (e.g. Hindi).
#3

Updated by shyouhei (Shyouhei Urabe) about 1 year ago

  • Related to Feature #12447: Integer#digits for extracting digits of place-value notation in any base added

Updated by shyouhei (Shyouhei Urabe) about 1 year ago

class Integer
  def delimited(n)
    div, mod = n.divmod(3)
    return (                        \
      digits(1000).lazy + [0].cycle \
    ) . first(div + 1)              \
      . reverse                     \
      . map {|i| '%03d' % i }       \
      . join(',')                   \
      . sub(/\A\d{#{3-mod}},?/, '')
  end
end


1234.delimited(8) #=> "0,001,234"

Updated by znz (Kazuhiro NISHIYAMA) about 1 year ago

Some printf can use %'d.

$ /usr/bin/printf "%'d\n" 1234
1,234

Updated by shyouhei (Shyouhei Urabe) about 1 year ago

znz (Kazuhiro NISHIYAMA) wrote:

Some printf can use %'d.

Yes but that's not what the OP wants.

% printf "%'07d\\n" 1234
001,234

The output is different.

Updated by shevegen (Robert A. Heiler) about 1 year ago

The python example seems quite concise to me:

format(1234, '08,') # => '0,001,234'
format(1234, '8,')  # => '   1,234'

I can not say how useful this may be though.

The method-names seem a bit strange to me - format() seems very generic
and delimited() is .... hmmm. I am not sure with what this is "de-limited".

Updated by shan (Shannon Skipper) about 1 year ago

class Integer
  def delimited(by: ',', digits: 0, padding: '0', every: 3)
    extra_padding_size = digits.to_int - Math.log10(self).floor.succ
    extra_padding = if extra_padding_size.positive?
      Array.new(extra_padding_size, padding.to_str.chr) 
    end

    (self.digits + extra_padding.to_a).each_slice(every).map do |triplet|
      triplet.reverse.join
    end.reverse.join(by.to_str)
  end
end

N = 4_200_000

N.delimited
#=> "4,200,000"

N.delimited(by: '_')
#=> "4_200_000"

N.delimited(every: 2, by: '_')
#=> "4_20_00_00"

N.delimited(digits: 12)
#=> "000,004,200,000"

N.delimited(digits: 12, padding: 'X')
#=> "XXX,XX4,200,000"

Updated by Hanmac (Hans Mackowiak) about 1 year ago

because it is locale-dependent as duerst said, i think it should only be part of an intl gem where you can control the locale

Updated by ko1 (Koichi Sasada) about 1 year ago

  • Assignee set to matz (Yukihiro Matsumoto)
  • Status changed from Open to Assigned

Updated by matz (Yukihiro Matsumoto) about 1 year ago

  • Status changed from Assigned to Rejected

I agree with duerst (Martin Dürst) and Hanmac (Hans Mackowiak)
It's handy but it cannot be in the standard library.

Matz.

Also available in: Atom PDF