Newbie - parse number into integer and decimal portions

D

dakota64

I am a newbie so please excuse the basic question. I have not been
able to find an answer to this. I am trying to modify an existing
application. The SQL database stores a value as a decimal. I need to
display the value in 2 text boxes, 1 for dollars and the other for
cents. Some of the numbers have a decimal value and some do not,
example - 1233, 44.55, 55.8.

I am using a datareader to retrieve the record . This is the code I
have

While dr.Read
Me.txtCustomerName.Text = dr(4)
Me.txtSSN.Text = dr(5)
Me.ddlState.SelectedValue = dr(6)
Me.txtMFDollar.Text = dr(7)
Me.txtMFCents.Text = dr(7)
Me.txtTLDollar.Text = dr(8)
Me.txtTLREfNumber.Text = dr(12)
Me.txtVLDollar.Text = dr(9)
Me.txtBankDollar.Text = dr(10)
Me.txtPhoenixDollar.Text = dr(11)
Me.txtVLREfNumber.Text = dr(12)
End While

I am trying separate the number for Me.txtMFDollar.text and
Me.txtMFCents.text

Thank you
 
R

rowe_newsgroups

The simplest (imo) way is to cast the datareader's value into a string
and use the split method.

Here's a Console Application that demonstrates the process:

///////////////
Module Module1

Sub Main()
Dim number As String() = "44.55".Split("."c)

Console.WriteLine("Integer Part: {0}", number(0))
Console.WriteLine("Decimal Part: {0}", number(1))

Console.Read()
End Sub

End Module
///////////////

Thanks,

Seth Rowe
 
A

Armin Zingler

dakota64 said:
I am a newbie so please excuse the basic question. I have not been
able to find an answer to this. I am trying to modify an existing
application. The SQL database stores a value as a decimal. I need
to display the value in 2 text boxes, 1 for dollars and the other
for cents. Some of the numbers have a decimal value and some do
not, example - 1233, 44.55, 55.8.

I am using a datareader to retrieve the record . This is the code I
have

While dr.Read
Me.txtCustomerName.Text = dr(4)
Me.txtSSN.Text = dr(5)
Me.ddlState.SelectedValue = dr(6)
Me.txtMFDollar.Text = dr(7)
Me.txtMFCents.Text = dr(7)
Me.txtTLDollar.Text = dr(8)
Me.txtTLREfNumber.Text = dr(12)
Me.txtVLDollar.Text = dr(9)
Me.txtBankDollar.Text = dr(10)
Me.txtPhoenixDollar.Text = dr(11)
Me.txtVLREfNumber.Text = dr(12)
End While

I am trying separate the number for Me.txtMFDollar.text and
Me.txtMFCents.text

Untested:

dim value, Dollars, Cents as decimal

value = directcast(dr(7), decimal) 'I guess it's decimal
dollars = int(value)
cents = (value - dollars) * 100D

txtMFDollar.text = dollars.tostring
txtMFCents.text = cents.tostring


Armin
 
P

Phill W.

dakota64 said:
I am trying to modify an existing application.
The SQL database stores a value as a decimal. I need to display the
value in 2 text boxes, 1 for dollars and the other for cents.

First things first.

Option Strict On

Find it. Turn it On.

Pretty much all of the code below will break, making you sort out the
Type Conversions that need to happen. IMHO, it's worth the effort so
that you understand what VB's doing on your behalf.
While dr.Read
Me.txtCustomerName.Text = dr(4)

For example:
dr(?) returns an Object; you have to convert it to the correct Type to
assign to a .Text property.

(Suggestion): Don't use column/field numbers - you never know when some
"enterprising Soul" will decide to add another field smack in the middle
of the record and trash all your index-based code.

Me.txtCustomerName.Text = dr("customer_name").ToString()
Me.txtMFDollar.Text = dr(7)
Me.txtMFCents.Text = dr(7)

Doing the job step-by-step:

Dim tempValue as Decimal = CDec( dr("mf") )
Dim sFormatted as String = tempValue.ToString( "0.00" )
Dim sBits as String() = sFormatted.Split( "." )

Me.txtMFDollar.Text = sBits(0)
Me.txtMFCents.Text = sBits(1)

HTH,
Phill W.
 
Top