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 a string into a decimal
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
convert a string into a decimal

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 15h16   #1
Ernst Tanaka
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut convert a string into a decimal

I am a new Ruby programmer. I am really amazed with the ease of
programming.

I seem not to be able to find a solution for the following task.

I want to convert an object with the class string into a object with the
class fixnum or decimal. So I can store the result in a Decimal(13,2)
sql field.


The value of the object string is "100,000,000.00" (including the comma
and the decimal point.

Appreciate all the at for hand!

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

  Réponse avec citation
Vieux 06/11/2007, 15h19   #2
Paulo Carvalho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: convert a string into a decimal

> a="100000000.00"
> a.to_f

=> 100000000.0


Ernst Tanaka wrote:
> I am a new Ruby programmer. I am really amazed with the ease of
> programming.
>
> I seem not to be able to find a solution for the following task.
>
> I want to convert an object with the class string into a object with the
> class fixnum or decimal. So I can store the result in a Decimal(13,2)
> sql field.
>
>
> The value of the object string is "100,000,000.00" (including the comma
> and the decimal point.
>
> Appreciate all the at for hand!
>
> Ernst


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

  Réponse avec citation
Vieux 06/11/2007, 15h29   #3
Ernst Tanaka
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: convert a string into a decimal

Paulo Carvalho wrote:
> > a="100000000.00"
> > a.to_f

> => 100000000.0
>
>

Thank you for the quick reaction.

to_f seems not to be the solution

a = "13,014,530"
a.to_f

>> 13.0



The result I am looking for is
13014530


Thanks again.

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

  Réponse avec citation
Vieux 06/11/2007, 15h31   #4
Jose francisco Gonzalez carmona
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: convert a string into a decimal

Ernst Tanaka wrote:
> I am a new Ruby programmer. I am really amazed with the ease of
> programming.
>
> I seem not to be able to find a solution for the following task.
>
> I want to convert an object with the class string into a object with the
> class fixnum or decimal. So I can store the result in a Decimal(13,2)
> sql field.
>
>
> The value of the object string is "100,000,000.00" (including the comma
> and the decimal point.
>
> Appreciate all the at for hand!
>
> Ernst


irb(main):005:0> "100,000,000.00".gsub(',','_').to_f
=> 100000000.0


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

  Réponse avec citation
Vieux 06/11/2007, 15h31   #5
Pat George
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: convert a string into a decimal

Ruby newb incoming:

Since his string is "100,000,000.00" to_f won't work:

irb(main):001:0> a = "100,000,000.00"
=> "100,000,000.00"
irb(main):002:0> a.to_f
=> 100.0

Only thing I could think of was to gsub! it first and get rid of the commas:

irb(main):001:0> a = "100,000,000.00"
=> "100,000,000.00"
irb(main):003:0> a.gsub!(/,/, '')
=> "100000000.00"
irb(main):004:0> a.to_f
=> 100000000.0

Any other way?

Paulo Carvalho wrote:
> > a="100000000.00"
> > a.to_f

> => 100000000.0
>
>
> Ernst Tanaka wrote:
>
>> I am a new Ruby programmer. I am really amazed with the ease of
>> programming.
>>
>> I seem not to be able to find a solution for the following task.
>>
>> I want to convert an object with the class string into a object with the
>> class fixnum or decimal. So I can store the result in a Decimal(13,2)
>> sql field.
>>
>>
>> The value of the object string is "100,000,000.00" (including the comma
>> and the decimal point.
>>
>> Appreciate all the at for hand!
>>
>> Ernst
>>

>
>



  Réponse avec citation
Vieux 06/11/2007, 15h33   #6
Ernst Tanaka
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: convert a string into a decimal


> irb(main):005:0> "100,000,000.00".gsub(',','_').to_f
> => 100000000.0



That did the trick! Thanks
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 15h55   #7
Armin Armbruster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: convert a string into a decimal

Pat George wrote:

> Any other way?


a.delete(',').to_f

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

  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 16h49.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14017 seconds with 15 queries