473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

beginner: VB.NET currency inputs

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
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

Nov 21 '05 #2
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
"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
10
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...
1
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...
0
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...
3
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...
1
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...
18
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...
25
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...
22
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.