Negative nums

T

Tim Wolak

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 =AD is a negative number. What is the best way to g=
et
my account balance and make it a negative number? I=B9m not sure I=B9m doi=
ng it
the correct way in this script.

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


--=20
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
 
J

Jano Svitok

Morning all,

I sent an email on this yesterday and needed to give you guys some examp= le
code. Below is some code I have for grabbing the numbers I need from li= nes
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 =AD is a negative number. What is the best way t= o get
my account balance and make it a negative number? I=B9m not sure I=B9m = doing it
the correct way in this script.

what about:
class Info
attr_reader :acct, :money

def initialize(filename)
@acct =3D File.new(filename, "r")
end
f =3D Info.new("Balances20080415.txt")
act =3D f.acct
act.each do |list|
#covert me to a string
futbal =3D list.to_s
#Pull accounts
office =3D futbal[22..24]
if office =3D=3D "RPT"
next
else
acctnum =3D futbal[24..28]
end
#Pull Liquidating values
lv =3D futbal[217..231]
#Pull LV Indicator
is_negative =3D futbal[215..215] =3D=3D '-'
value =3D lv.to_f/1000
value =3D -value if is_negative
puts acctnum," ",value

or (though not very readable)

value =3D futbal[217..231].to_f / ((futbal[215..215] =3D=3D '-') ? -1000 : =
1000)

or a bit better:

value =3D futbal[217..231].to_f / 1000 * ((futbal[215..215] =3D=3D '-') ? -=
1 : 1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Hashes 6
Hash keys 3
n00b question 4
MiniQuiz : Renesting Nodes (OWLScratch) 1
Fun with Permutations 3
[QUIZ SOLUTION] Happy Numbers (#93) 3
One Small step one infinite leap 1
[SUMMARY] To Excel (#17) 1

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top