PC Review


Reply
Thread Tools Rate Thread

beginner: VB.NET currency inputs

 
 
jcnews
Guest
Posts: n/a
 
      12th Nov 2004
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


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      12th Nov 2004
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" <(E-Mail Removed)> schreef in bericht
news:nzZkd.24640$(E-Mail Removed)...
>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
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      12th Nov 2004
"Cor Ligthert" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      12th Nov 2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add new currency Symbol in Format/Cell/Currency NOORZAD Microsoft Excel Misc 2 22nd Jun 2009 07:59 AM
Currency Datatype Really Good to Use For Non-currency Fields? tbl Microsoft Access Database Table Design 4 10th Jul 2006 02:58 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= Microsoft ASP .NET 7 3rd Feb 2005 02:47 PM
Beginner: Parse? Need to process currency input. Josh Microsoft VB .NET 2 11th Dec 2004 06:38 PM
Conversion from currency value to currency text format gdselva Microsoft Excel Programming 2 18th Aug 2004 10:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:06 AM.