PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Convert bignum to numeric and formatting
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Convert bignum to numeric and formatting

Réponse
 
LinkBack Outils de la discussion
Vieux 05/01/2008, 17h07   #1
Damaris Fuentes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Convert bignum to numeric and formatting

Hi you all,

I'm trying to format a Bignum such as 1012345556 into 1.012.345.556
(separating the thousands).
I've seen the extension of Numeric [1] can do that but, how can I
convert my Bignum to a Numeric?

Thanks.

[1] http://extensions.rubyforge.org/rdoc...s/Numeric.html
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 05/01/2008, 17h15   #2
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert bignum to numeric and formatting

Alle sabato 5 gennaio 2008, Damaris Fuentes ha scritto:
> Hi you all,
>
> I'm trying to format a Bignum such as 1012345556 into 1.012.345.556
> (separating the thousands).
> I've seen the extension of Numeric [1] can do that but, how can I
> convert my Bignum to a Numeric?
>
> Thanks.
>
> [1] http://extensions.rubyforge.org/rdoc...s/Numeric.html


You don't need to convert a Bignum into a Numeric because it already is:
Bignum is a subclass of Integer, which is a Subclass of Numeric.

Stefano

  Réponse avec citation
Vieux 05/01/2008, 17h33   #3
Damaris Fuentes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert bignum to numeric and formatting

Hi,

> You don't need to convert a Bignum into a Numeric because it already is:
> Bignum is a subclass of Integer, which is a Subclass of Numeric.


I get this:
"undefined method `format_s' for 10067064106:Bignum"

So my question is, I'm afraid, how can I install the Numeric extension?
(I always thought the extensions come with the Ruby installation...)

Thanks.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 05/01/2008, 17h50   #4
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert bignum to numeric and formatting

Alle sabato 5 gennaio 2008, Damaris Fuentes ha scritto:
> Hi,
>
> > You don't need to convert a Bignum into a Numeric because it already is:
> > Bignum is a subclass of Integer, which is a Subclass of Numeric.

>
> I get this:
> "undefined method `format_s' for 10067064106:Bignum"
>
> So my question is, I'm afraid, how can I install the Numeric extension?
> (I always thought the extensions come with the Ruby installation...)
>
> Thanks.


If you have already rubygems installed, you can install extensions through it:

gem install -r extensions

(if you're on unix, you'll need to use sudo to do this).

If you don't have rubygems installed, you can install it (look at the
rubyforge page for rubygems for how to do it) or install extensions by hand.
To do this, go to the extensions project page on rubyforge(
http://rubyforge.org/projects/extensions/ ), follow the download link and
download the tgz file. At this point, you need to extract the files in the
tgz file and put them somewhere ruby will look for them (you can get a list
of such places looking at the $: global variable from irb). Depending on the
operating system you use, these paths may vary. For instance, on my gentoo
system, they are:

/usr/lib/ruby/site_ruby/1.8
/usr/lib/ruby/site_ruby/1.8/i686-linux
/usr/lib/ruby/site_ruby
/usr/lib/ruby/1.8
/usr/lib/ruby/1.8/i686-linux

The most appropriate place in this case should be /usr/lib/ruby/site_ruby/1.8.

Another option is to put the files in any directory and add it to the ruby
load path either via the environment variable RUBYLIB or using the -L switch
when calling ruby or modifying the $: variable from inside your ruby program.

I hope this s

Stefano

  Réponse avec citation
Vieux 05/01/2008, 18h10   #5
Damaris Fuentes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert bignum to numeric and formatting

Ok,

I've executed:
gem install -r extensions

And "extensions-0.6.0" has been installed (thanks! ) (I've checked
it), but the error of
"undefined method `format_s' for 10067064106:Bignum"
still appears.

(I'm working with Rails)

Stefano Crocco wrote:
> Alle sabato 5 gennaio 2008, Damaris Fuentes ha scritto:
>>
>> Thanks.

>
> If you have already rubygems installed, you can install extensions
> through it:
>
> gem install -r extensions
>
> (if you're on unix, you'll need to use sudo to do this).
>
> If you don't have rubygems installed, you can install it (look at the
> rubyforge page for rubygems for how to do it) or install extensions by
> hand.
> To do this, go to the extensions project page on rubyforge(
> http://rubyforge.org/projects/extensions/ ), follow the download link
> and
> download the tgz file. At this point, you need to extract the files in
> the
> tgz file and put them somewhere ruby will look for them (you can get a
> list
> of such places looking at the $: global variable from irb). Depending on
> the
> operating system you use, these paths may vary. For instance, on my
> gentoo
> system, they are:
>
> /usr/lib/ruby/site_ruby/1.8
> /usr/lib/ruby/site_ruby/1.8/i686-linux
> /usr/lib/ruby/site_ruby
> /usr/lib/ruby/1.8
> /usr/lib/ruby/1.8/i686-linux
>
> The most appropriate place in this case should be
> /usr/lib/ruby/site_ruby/1.8.
>
> Another option is to put the files in any directory and add it to the
> ruby
> load path either via the environment variable RUBYLIB or using the -L
> switch
> when calling ruby or modifying the $: variable from inside your ruby
> program.
>
> I hope this s
>
> Stefano


--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 05/01/2008, 18h14   #6
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert bignum to numeric and formatting

Alle sabato 5 gennaio 2008, Damaris Fuentes ha scritto:
> Ok,
>
> I've executed:
> gem install -r extensions
>
> And "extensions-0.6.0" has been installed (thanks! ) (I've checked
> it), but the error of
> "undefined method `format_s' for 10067064106:Bignum"
> still appears.
>
> (I'm working with Rails)
>
> Stefano Crocco wrote:


Yes, I forgot to add that you need to use

require 'extensions/numeric'

in the file where you use format_s

Stefano

  Réponse avec citation
Vieux 05/01/2008, 18h28   #7
Damaris Fuentes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert bignum to numeric and formatting

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.

Stefano Crocco wrote:
> Alle sabato 5 gennaio 2008, Damaris Fuentes ha scritto:
>> (I'm working with Rails)
>>
>> Stefano Crocco wrote:

>
> Yes, I forgot to add that you need to use
>
> require 'extensions/numeric'
>
> in the file where you use format_s
>
> Stefano


--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 05/01/2008, 19h11   #8
Tim Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert bignum to numeric and formatting

Damaris Fuentes wrote:
> Hi you all,
>
> I'm trying to format a Bignum such as 1012345556 into 1.012.345.556
> (separating the thousands).


Leaving aside the Numeric extension for a minute, this bit of code
solves the problem.

x = 12345678901234
t = x.to_s.reverse.gsub(/(\d){3}(?=\d)/) {|p| p + '.'}.reverse
puts t

I've seen other regexp solutions to this problem on this list, but the
above code is what I came up with on the spur of the moment.

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h18.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15682 seconds with 16 queries