# Locale number formatting

**URL:** <https://rubytalk.org/t/locale-number-formatting/65438>\
**Category:** ruby-talk\
**Created:** [9 May 2012 21:40 UTC](https://rubytalk.org/t/locale-number-formatting/65438 "2012-05-09T21:40:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [9 May 2012 21:40 UTC](https://rubytalk.org/t/locale-number-formatting/65438/1 "2012-05-09T21:40:03Z")

</div>

I've searched and searched for this and have not found an answer.

ruby 1.9.3p194 (2012-04-20) [i386-mingw32]  
MSWindows 7

How do you format a number to a current locale? (ie: 1000 -\> 1,000 for  
'en')

I found the gem package "locale" (locale-2.0.5), but after installation  
the advised simplest example in the HowTo:

> **···**
>
> ----------------------  
> require 'locale'
> 
> Locale.current  
> ----------------------
> 
> displays when run
> 
> ----------------------  
> C:/Ruby193/lib/ruby/site\_ruby/1.9.1/rubygems/custom\_require.rb:36:in  
> require': cannot load such file -- dl/win32 (LoadError)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from  
> C:/Ruby193/lib/ruby/site\_ruby/1.9.1/rubygems/custom\_require.rb:36:in  
> `require'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from  
> C:/Ruby193/lib/ruby/site\_ruby/1.9.1/locale/driver/win32.rb:16:in `\<top  
> (required)\>'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from  
> C:/Ruby193/lib/ruby/site\_ruby/1.9.1/rubygems/custom\_require.rb:36:in  
> `require'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Ruby193/lib/ruby/site\_ruby/1.9.1/rubygems  
> custom\_require.rb:36:in `require'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Ruby193/lib/ruby/site\_ruby/1.9.1/locale.rb:32:in  
> `require\_driver'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Ruby193/lib/ruby/site\_ruby/1.9.1/locale.rb:74:in `init'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Ruby193/lib/ruby/site\_ruby/1.9.1/locale.rb:90:in  
> `driver\_module'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Ruby193/lib/ruby/site\_ruby/1.9.1/locale.rb:178:in  
> `current'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Ruby193/lib/ruby/site\_ruby/1.9.1/locale.rb:212:in  
> `candidates'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from locale.rb:4:in `\<main\>'  
> ----------------------
> 
> Even so, the description of the package leaves no clue as to how or even  
> if this package provides a way to format data to a locale.
> 
> Any help with this would be greatly appreciated.
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [15 May 2012 23:45 UTC](https://rubytalk.org/t/locale-number-formatting/65438/2 "2012-05-15T23:45:39Z")

</div>

No answers? For such an incredibly common and required function?  
Really??

After a far-too-long search through web pages, blogs, and gem packages,  
I can now definitively say that this is one of the worst documented  
languages in history.

It seems, no guarantee here, that localization has centered around the  
R18n package found at "[http://r18n.rubyforge.org/](http://r18n.rubyforge.org/)". Again, the  
documentation sucks.

Apparently they refer to a standard Ruby interpreter (No Rails, Sinatra,  
etc.) as a "desktop" installation. You can install the package via gem  
with "gem install r18n-desktop". It will install two packages and will  
gack on the documentation with conversion errors (It seems a Ruby 1.9.x  
thing. I still have no idea why.).

The attached ruby script demonstrates locale data formatting (Not  
documented on the site). This will automatically create an 'i18n' helper  
object, configured to the current locale, with an 'l()' method that  
performs the locale formatting of a given object.

> **···**
>
> --------------------------------------  
> Output for a en-US locale
> 
> Locale: American English  
> 12,345,678.9  
> 05/15/2012 04:06 PM  
> --------------------------------------  
> Output for a de-DE locale
> 
> Locale: de\_de  
> 12.345.678,9  
> 15.05.2012 16:30  
> --------------------------------------  
> I do not know why there is no localized title for the de-DE locale. Seem  
> kind of insulting.
> 
> I have no idea if there are any difference between the 'i18n' and 'r18n'  
> objects. The 'r18n' object can be used in place of the 'i18n' object in  
> the attached script with the same results.
> 
> So, at least this works and does the desired job. Hopefully this will  
> save someone else from unnecessarily burning man-hours like I had to.
> 
> Enjoy!
> 
> Attachments:  
> [http://www.ruby-forum.com/attachment/7393/R18n\_Example.rb](http://www.ruby-forum.com/attachment/7393/R18n_Example.rb)
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [15 May 2012 23:52 UTC](https://rubytalk.org/t/locale-number-formatting/65438/3 "2012-05-15T23:52:57Z")

</div>

Thanks for following up! I just checked, and your post comes up as the  
third google hit for "ruby number locale", so as you say, hopefully  
the next person down the line will have an easier time.

martin

> **···**
>
> On Tue, May 15, 2012 at 4:45 PM, Llelan D. \<lists@ruby-forum.com\> wrote:
> 
> > No answers? For such an incredibly common and required function?  
> > Really??
> > 
> > After a far-too-long search through web pages, blogs, and gem packages,  
> > I can now definitively say that this is one of the worst documented  
> > languages in history.
> > 
> > It seems, no guarantee here, that localization has centered around the  
> > R18n package found at "[http://r18n.rubyforge.org/&quot;\](http://r18n.rubyforge.org/&quot;%5C). Again, the  
> > documentation sucks.
> > 
> > Apparently they refer to a standard Ruby interpreter (No Rails, Sinatra,  
> > etc.) as a "desktop" installation. You can install the package via gem  
> > with "gem install r18n-desktop". It will install two packages and will  
> > gack on the documentation with conversion errors (It seems a Ruby 1.9.x  
> > thing. I still have no idea why.).
> > 
> > The attached ruby script demonstrates locale data formatting (Not  
> > documented on the site). This will automatically create an 'i18n' helper  
> > object, configured to the current locale, with an 'l()' method that  
> > performs the locale formatting of a given object.
> > 
> > --------------------------------------  
> > Output for a en-US locale
> > 
> > Locale: American English  
> > 12,345,678.9  
> > 05/15/2012 04:06 PM  
> > --------------------------------------  
> > Output for a de-DE locale
> > 
> > Locale: de\_de  
> > 12.345.678,9  
> > 15.05.2012 16:30  
> > --------------------------------------  
> > I do not know why there is no localized title for the de-DE locale. Seem  
> > kind of insulting.
> > 
> > I have no idea if there are any difference between the 'i18n' and 'r18n'  
> > objects. The 'r18n' object can be used in place of the 'i18n' object in  
> > the attached script with the same results.
> > 
> > So, at least this works and does the desired job. Hopefully this will  
> > save someone else from unnecessarily burning man-hours like I had to.
> > 
> > Enjoy!
> > 
> > Attachments:  
> > [http://www.ruby-forum.com/attachment/7393/R18n\_Example.rb](http://www.ruby-forum.com/attachment/7393/R18n_Example.rb)
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
