Convert To Currency in Visual Basic
-
Monday, May 25, 2009 7:21 PM
yes its me again....
How would i go about converting a number such as 17.463 to USD? It should be $17.47. Can someone give me the code please?
my code is as follows:
If ckboxDis.Checked = True ThenDim Total = (CDbl(txtItem1.Text) + CDbl(txtItem2.Text) + CDbl(txtItem3.Text) + CDbl(txtItem4.Text) + CDbl(txtItem5.Text) + CDbl(txtItem6.Text) + CDbl(txtItem7.Text) + CDbl(txtItem8.Text) + CDbl(txtItem9.Text) + CDbl(txtItem10.Text) + CDbl(txtDelCharge.Text)) * 0.9 * 1.05
lblTotal.Text = Total.ToString()
End If
If ckboxDis.Checked = False Then
Dim Total = (CDbl(txtItem1.Text) + CDbl(txtItem2.Text) + CDbl(txtItem3.Text) + CDbl(txtItem4.Text) + CDbl(txtItem5.Text) + CDbl(txtItem6.Text) + CDbl(txtItem7.Text) + CDbl(txtItem8.Text) + CDbl(txtItem9.Text) + CDbl(txtItem10.Text) + CDbl(txtDelCharge.Text)) * 1.05
lblTotal.Text = Total.ToString()
- Edited by racecar_12345 Monday, May 25, 2009 7:48 PM making post shorter
All Replies
-
Monday, May 25, 2009 7:53 PM
ok, I'll explain it a bit.
When the check box is checked, it adds up the values (from txtItem1 to txtItem10) then applies a 10% discount, then it charges 5% tax.
When the check box is not checked, it adds up the values (from txtItem1 to txtItem10) then it charges 5% tax.
What i need help with: I need the output values to be in currency format. USD or CAD ($)
Anyideas?
Why be stuck in the limits of other people, make your own programs, run things the way you want. -
Monday, May 25, 2009 7:56 PM
Use the decimal type instead of double, he's just a brief snippet. Then, Visual Basic has a nifty function called FormatNumber that will format numbers for you and give you the option on how many decimal places to show, will put the commas in for you (group the digits) and optional parenthhesis if it's negative (instead of a - sign):
Dim cash As Decimal = 1.47 + 2.03 Dim cashDisplay As String = "$" & FormatNumber(cash, 2, TriState.False, TriState.True, TriState.True) MsgBox("Unformatted: " & cash) MsgBox("Formatted: " & cashDisplay) -
Monday, May 25, 2009 8:14 PMFrom your example, it looks like you want to round up to the next cent. You can use the Math.Ceiling method to do that. Using "C2" as the format string in the ToString method will format the number as currency with two decimal places. I think the following will do what you want. However, if these numbers are all currency, you might consider using Decimal rathr than Double variables.
Dim Total = (CDbl(txtItem1.Text) + CDbl(txtItem2.Text) + CDbl(txtItem3.Text) + CDbl(txtItem4.Text) + CDbl(txtItem5.Text) + CDbl(txtItem6.Text) + CDbl(txtItem7.Text) + CDbl(txtItem8.Text) + CDbl(txtItem9.Text) + CDbl(txtItem10.Text) + CDbl(txtDelCharge.Text)) * 1.05 If ckboxDis.Checked = True Then Total *= 0.9 Total = Math.Ceiling(Total * 100) / 100 lblTotal.Text = Total.ToString("C2") -
Monday, May 25, 2009 9:03 PM
This version uses Decimal variables and also uses Decimal.TryParse to check that the contents of the textboxes are valid decimal numbers (I've simply made it skip any invalid numbers, you should probably do something to tell the user to enter a valid number). I also used a loop to process the 10 TextBoxes (txtItem1 through txtItem10).
Dim total, price As Decimal For i = 1 To 2 Dim tb As TextBox = CType(Me.Controls("txtItem" & i.ToString), TextBox) If Decimal.TryParse(tb.Text, price) Then total += price Next If Decimal.TryParse(Me.txtDelCharge.Text, price) Then total += price total *= 1.05D If Me.ckboxDis.Checked Then total *= 0.9D total = Math.Ceiling(total * 100) / 100 Me.lblTotal.Text = total.ToString("C2")- Proposed As Answer by Cor LigthertMVP Tuesday, May 26, 2009 8:36 AM
- Marked As Answer by Martin Xie - MSFT Monday, June 01, 2009 6:49 AM
-
Wednesday, May 27, 2009 4:04 AM
okay maybe im just dumb, but i just cant get it to work. I'm going to try to make this as simple as possible so I can try to get a simple answer lol.
how do i get "lblTotal" to come out in two decminal places that always rounds up?
ex:
2.1 ---> 2.10
40.121--> 40.13
dont worry about the '$' symbol, I can add it in the designer.
Why be stuck in the limits of other people, make your own programs, run things the way you want. -
Wednesday, May 27, 2009 9:54 AMModerator
Thank you for Blackwood's useful help.
Hi Racecar_12345,
I've reproduced Blackwood's code. It can work well and can convert "lblTotal" into two decimal places that always rounds up.
Here is the code that I reproduced in my project:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim total, price As Decimal For i = 1 To 10 Dim tb As TextBox = CType(Me.Controls("txtItem" & i.ToString), TextBox) If Decimal.TryParse(tb.Text, price) Then total += price Next If Decimal.TryParse(Me.txtDelCharge.Text, price) Then total += price total *= 1.05D If Me.ckboxDis.Checked Then total *= 0.9D total = Math.Ceiling(total * 100) / 100 total = Math.Round(total, 2) Me.lblTotal.Text = total.ToString("C2") End Sub
If you have any concern on this issue, please feel free to let me know.
Thanks
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Edited by YiChun ChenModerator Wednesday, May 27, 2009 9:55 AM typo
- Marked As Answer by Martin Xie - MSFT Monday, June 01, 2009 6:51 AM
-
Friday, May 29, 2009 6:15 PM
okay maybe im just dumb, but i just cant get it to work. I'm going to try to make this as simple as possible so I can try to get a simple answer lol.
how do i get "lblTotal" to come out in two decminal places that always rounds up?
ex:
2.1 ---> 2.10
40.121--> 40.13
dont worry about the '$' symbol, I can add it in the designer.
Why be stuck in the limits of other people, make your own programs, run things the way you want.
In the code I provided above, the following line takes care of rounding up to the next cent
total = Math.Ceiling(total * 100) / 100
Math.Ceiling rounds up to the next whole number, so I multiplied the total amount by 100 (so the cents become dollars) before using Math.Ceiling, then I divided by 100 afterwards (changing the dollars back to cents).

