Wednesday, June 24, 2009

Formatting Decimals In Visual Basic

got a project from my programming book, which using for this subject.
now, when i put in the dollar amount, and click calculate, it does not show
the orrigional prices in a decimal format.
so put in a variable called Dim decOriginalPrice as decimal = 0
and then in my code, did put in to show the format, such as
tbOrigionalPriceTextBox.Text = decOrigionalPrice.ToString("C")
for the decimal passing, but it still just shows up as say for example
125.55 instead of $125.00, but my discounted price amount and the total
price is in dollars and cents.
so my question is:

is there some code to format the amount to dollars and cents?
for the Origional Price.
did put those variables and code in, but did not work so took them out.
any ideas how i can fix this one?
the application is to enter a price, enter the description for to recover
callateral from a debt collector.
wen you click the calculate button, it sets focus on the origional price
text box, then it then shows the origional price, then in a group box and
read only boxes, the discount which is 10% and formatted to the dollars and
cents, then the total price of the discounted goods in dollars and cents
format.
will paste the code below.
if any one can help me how to fix this strange and queer oddity or some
thing i am missing, as rebuilding my projects from 2008 to 2005.
cheers Marvin.

'Program: Discount
'Programmer: Marvin Hunkin
'Date: Tuesday September 23 2008
'Description: Calculate Discount Amounts For Lennie's Baol Bonds
' Then display the discounted amount, the description, and total amount of
discount at 10% for the total cost of the item being held

Public Class frmDiscount

Private Sub btnCalculateButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnCalculateButton.Click

Dim constDiscountRate As Decimal = 0.1
Dim decDiscountAmount As Decimal = 0
Dim decDiscountedPrice As Decimal = 0

If IsNumeric(tbOriginalPriceTextBox.Text) Then
decDiscountAmount = CDec(tbOriginalPriceTextBox.Text) *
constDiscountRate
decDiscountedPrice = CDec(tbOriginalPriceTextBox.Text) -
decDiscountAmount
tbDiscountAmountTextBox.Text = decDiscountAmount.ToString("C")
tbDiscountedPriceTextBox.Text = decDiscountedPrice.ToString("C")
Else
MessageBox.Show("Error: Price Field Not Numeric")
tbOriginalPriceTextBox.Focus()
GoTo ProcExit

ProcExit:
Return

End If
tbOriginalPriceTextBox.Focus()

End Sub

Private Sub btnClearButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnClearButton.Click

tbOriginalPriceTextBox.Clear()
tbDiscountAmountTextBox.Clear()
tbDiscountedPriceTextBox.Clear()
tbDescriptionTextBox.Clear()
tbOriginalPriceTextBox.Focus()

End Sub

Private Sub btnExitButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnExitButton.Click

'Exit the project

Me.Close()

End Sub
End Class

Your Title