# Negative nums

**URL:** <https://www.ruby-forum.com/t/negative-nums/135478>\
**Category:** Ruby\
**Created:** [April 22, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/negative-nums/135478 "2008-04-22T16:18:08Z")\
**Posts on this page:** 3\
**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 22, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/negative-nums/135478/1 "2008-04-22T16:18:08Z")

</div>

Morning all,

I sent an email on this yesterday and needed to give you guys some  
example  
code. Below is some code I have for grabbing the numbers I need from  
lines  
in a text file that we receive via FTP. My big concern is the negative  
numbers for accounts which are indicated by a position in the file where  
0  
is a postive number and Â­ is a negative number. What is the best way to  
get  
my account balance and make it a negative number? IÂ¹m not sure IÂ¹m  
doing it  
the correct way in this script.

class Info  
attr\_reader :acct, :money

def initialize(filename)  
@acct = File.new(filename, “r”)  
end  
f = Info.new(“Balances20080415.txt”)  
act = f.acct  
act.each do |list|  
#covert me to a string  
futbal = list.to\_s  
#Pull accounts  
office = futbal[22…24]  
if office == “RPT”  
next  
else  
acctnum = futbal[24…28]  
end  
#Pull Liquidating values  
lv = futbal[217…231]  
#Pull LV Indicator  
lvind = futbal[215…215]  
#if Negitave vlaues  
if lvind == “-”  
lvnegfloat = lv.to\_f/1000  
print acctnum," ",lvind, lvnegfloat, “\n”  
#else Positive Values  
else  
lvposflt = lv.to\_f/1000  
print acctnum, " ", lvposflt, “\n”  
end  
end  
end

---

<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 23, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/negative-nums/135478/2 "2008-04-23T15:59:45Z")

</div>

Hi –

On Tue, 22 Apr 2008, Tim W. wrote:

> class Info  
> attr\_reader :acct, :money
> 
> def initialize(filename)  
> @acct = File.new(filename, “r”)  
> end  
> f = Info.new(“Balances20080415.txt”)

It’s extremely unusual (I don’t think I’ve ever seen it) to put  
instantiation and scripting code (for lack of a better term) inside a  
class definition. It will get executed, but it’s really odd-looking  
🙂

Question: Can you provide some sample lines of input? I can’t quite  
get a fix on what the lines look like. I’m wondering whether scanf  
might help you.

David

---

<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 23, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/negative-nums/135478/3 "2008-04-23T15:35:36Z")

</div>

On Tue, Apr 22, 2008 at 4:17 PM, Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Morning all,
> 
> I sent an email on this yesterday and needed to give you guys some example  
> code. Below is some code I have for grabbing the numbers I need from lines  
> in a text file that we receive via FTP. My big concern is the negative  
> numbers for accounts which are indicated by a position in the file where 0  
> is a postive number and ­ is a negative number. What is the best way to get  
> my account balance and make it a negative number? I¹m not sure I¹m doing it  
> the correct way in this script.

what about:

> ```
> futbal = list.to_s
> #Pull accounts
> office = futbal[22..24]
> if office == "RPT"
> next
> else
> acctnum = futbal[24..28]
> end
> #Pull Liquidating values
> lv = futbal[217..231]
> #Pull LV Indicator
> 
> ```

```
        is_negative = futbal[215..215] == '-'
        value = lv.to_f/1000
        value = -value if is_negative
        puts acctnum," ",value

```

> end

or (though not very readable)

value = futbal[217…231].to\_f / ((futbal[215…215] == ‘-’) ? -1000 :  
1000)

or a bit better:

value = futbal[217…231].to\_f / 1000 \* ((futbal[215…215] == ‘-’) ? -1 :  
1)
