beginner: VB.NET currency inputs

J

jcnews

I am writing a 'wage calculator' program where the user inputs a dollar
amount and then calculations are performed. I need to make sure that the
input is only something like this: "$12.42", or "$4", or "5.30", and have
the output always be in this format: "$#.##". Currently, the output
sometimes looks like this: "$54.321", or "$512.2".

This is what I have come up with by researching in books and using the
built-in help. It's actually just a conversion of a simple java program
that I saw in a book. I'm not really sure if I am doing everything right,
but the program works except for that one problem.

Any advice would be appreciated.

---------------------------------------------------------------------
The entire sub is here:
---------------------------------------------------------------------
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCalculate.Click

' Anything above 40 hours will be overtime
Const MaxHours As Integer = 40

' Declare variables -- input
Dim dHourlyWage As Double
Dim dHoursWorked As Double

' Declare variables -- calculations
Dim dRegularHours As Double
Dim dOvertimeHours As Double
Dim dGrossWages As Double

' Declare variables -- output
Dim strRegularHours As String
Dim strOvertimeHours As String
Dim strResult As String

' Place input into the variables, converting from strings to doubles
dHourlyWage = Double.Parse(txtHourlyWage.Text,
Globalization.NumberStyles.Currency)
dHoursWorked = Double.Parse(txtHoursWorked.Text,
Globalization.NumberStyles.Currency)

' Calculate if there is any overtime (over 40 hours)
If dHoursWorked <= MaxHours Then
dRegularHours = dHourlyWage * dHoursWorked
dOvertimeHours = 0
dGrossWages = dRegularHours
Else
dRegularHours = (dHourlyWage * MaxHours)
dOvertimeHours = (dHoursWorked - MaxHours) * (1.5 * dHourlyWage)
dGrossWages = dRegularHours + dOvertimeHours
End If

' Convert doubles to strings for display on labels
strResult = System.Convert.ToString(dGrossWages)
strRegularHours = System.Convert.ToString(dRegularHours)
strOvertimeHours = System.Convert.ToString(dOvertimeHours)

' Display total Gross Hours
lblResult.Text = "$" & strResult

' Show hidden labels
lblSummary.Visible = True
lblRegularResult.Visible = True
lblOvertimeResult.Visible = True
lblRegularHours.Visible = True
lblOvertimeHours.Visible = True

' Display results on Summary labels
lblRegularResult.Text = "$" & strRegularHours
lblOvertimeResult.Text = "$" & strOvertimeHours
 
C

Cor Ligthert

Hi JCnews,

Did you already tried it like this?
\\\\
If IsNumeric(TextBox1.Text) Then
TextBox1.Text = (2 * CDec(TextBox1.Text)).ToString("C")
'calculation as sample
Else
MessageBox.Show("false")
End If
///

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Did you already tried it like this?
\\\\
If IsNumeric(TextBox1.Text) Then
TextBox1.Text = (2 * CDec(TextBox1.Text)).ToString("C")
'calculation as sample
Else
MessageBox.Show("false")
End If

This will "fail" if the user enters a numeric test that cannot be
represented in the 'Decimal' data type.
 
C

Cor Ligthert

Herfried,
This will "fail" if the user enters a numeric test that cannot be
represented in the 'Decimal' data type.
I know however it is just a sample how to set that "currencysymbol", not to
evaluate the right inserted value.

Cor
 

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

Top