[ruby-core:93841] [Ruby master Feature#16011] Digit grouping

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-79725.20190719055716.50524791b62c5377@ruby-lang.org>
Issue #16011 has been updated by shyouhei (Shyouhei Urabe).


```ruby
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"
```

----------------------------------------
Feature #16011: Digit grouping
https://bugs.ruby-lang.org/issues/16011#change-79725

* Author: svnpenn (Steven Penny)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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
    }



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.