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 6 3294
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
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
"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/>
"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/>
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Willing 2 Learn |
last post by:
I'm still having trouble getting my program to do arithmetic in
cents(keeping all #'s) then convert the answer in a format of dollars &
cents. The main program should add, subtract, scalar...
|
by: Shawn |
last post by:
Hello all,
I apologize as I am sure this has probably been dealth with before...
but I am doing an exercise from "Practical C Programming" and I have
been unable to get it to work perfectly due to...
|
by: Manuel Canas |
last post by:
I'm stuck here guys, need some help.
I have a form with a couple fo tabs on it. I load a table in each for each
tab. I populate a listbox with a list of names. Then I use currency manager
to...
|
by: jcnews |
last post by:
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...
|
by: Josh |
last post by:
I am writing a program where the user inputs currency in US dollars. I want
the program to only accept valid currency input, converting the string into
the proper type of variable (double?), and...
|
by: c_beginner |
last post by:
yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.
Write a program to calculate the...
|
by: Boris Yeltsin |
last post by:
OK, I have a database table, it has prices of products in it, like so:
ProductPrice MONEY
ProductIsoCurrencyCode CHAR(3)
Now, both CultureInfo and RegionInfo have...
|
by: mereba |
last post by:
Hello
My country Ghana is changing its currency. I want to write a small
programme in C++ that can covert from the old currency into the new
one. I would like this programme to run behind a simple...
|
by: ddg_linux |
last post by:
I have been reading about and doing a lot of php code examples from
books but now I find myself wanting to do something practical with
some of the skills that I have learned.
I am a beginner php...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |