# \<no subject\>

**URL:** <https://www.ruby-forum.com/t/no-subject/135400>\
**Category:** Ruby\
**Created:** [April 21, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/no-subject/135400 "2008-04-21T21:49:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [April 21, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/no-subject/135400/1 "2008-04-21T21:49:03Z")

</div>

Afternoon all,

I have a set of files that are downloaded via ftp which I iterate over  
and  
grab account numbers are their balances. I have been able to iterate  
over  
the individual lines and get my data however the account balances are  
gather  
by getting their specific position on current line and the position just  
before the account balance is a “0” or a “-”, which indicates if the  
balance  
is a positive or a negative number.

My issue is what is the best way to have ruby indicate this float number  
as  
a negative number so when I compare account balances from the current  
day  
and the previous that the difference is calculated correctly?

Thanks in advance.  
Tim

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [April 21, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/no-subject/135400/2 "2008-04-21T22:04:27Z")

</div>

On Mon, Apr 21, 2008 at 9:48 PM, Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My issue is what is the best way to have ruby indicate this float number as  
> a negative number so when I compare account balances from the current day  
> and the previous that the difference is calculated correctly?
> 
> Thanks in advance.  
> Tim

1. It’s easier to help if you provide some samples from the data,  
together with expected results.
2. Have a look at Regexp [1] and String#to\_f
3. You may want to use BigDecimal [2] instead of floats for money.  
floats are internally stored as binary fractions (obviously) so you  
might have problems with loosing tiny fractions of numbers.

[1] [http://ruby-doc.org/docs/ProgrammingRuby/html/tut\_stdtypes.html#S4](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html#S4)  
[2] [http://ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [April 21, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/no-subject/135400/3 "2008-04-21T22:06:12Z")

</div>

On Mon, Apr 21, 2008 at 1:48 PM, Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> is a positive or a negative number.  
> This message has been scanned for viruses and  
> dangerous content by MailScanner, and is  
> believed to be clean.

to\_f

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [April 21, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/no-subject/135400/4 "2008-04-21T23:11:09Z")

</div>

On 21.04.2008 21:48, Tim W. wrote:

> Afternoon all,

Good night.

> I have a set of files that are downloaded via ftp which I iterate over and  
> grab account numbers are their balances. I have been able to iterate over  
> the individual lines and get my data however the account balances are gather  
> by getting their specific position on current line and the position just  
> before the account balance is a “0” or a “-”, which indicates if the balance  
> is a positive or a negative number.
> 
> My issue is what is the best way to have ruby indicate this float number as  
> a negative number so when I compare account balances from the current day  
> and the previous that the difference is calculated correctly?

Floats can be negative and positive. There is nothing extra you need to  
do.

Note however that if you want correctness with currency values it is  
better to use BigDecimal instead of Float because of the increased  
exactness. Floats may be sufficient in your case but you should keep in  
mind that there are some issues lurking.

Kind regards

robert
