By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,034 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,034 IT Pros & Developers. It's quick & easy.

beginner: VB.NET currency inputs

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"jcnews" <jc****@earthlink.net> schreef in bericht
news:nz*****************@newsread1.news.pas.earthl ink.net...
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

Nov 21 '05 #2

P: n/a
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

"jcnews" <jc****@earthlink.net> schreef in bericht
news:nz*****************@newsread1.news.pas.earthl ink.net...
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

Nov 21 '05 #3

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
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
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.