Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all visitors,

I have problem with formatting the string to currency in vb.net. I have a text box and it will show the currency.
I used the Leave Event of text box, when I left cursor from the text box that currency just show the number and 2 decimal places( 12234.00) and there is no currency symbol.

Following is my code in Leave Event:
Private Sub txtprice_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtprice.Leave

        '12234.00
        Me.txtprice.Text = FormatCurrency(txtprice.Text)

End Sub


But when I use with button Click Even. The currency shows normally($ 123,434,89), even though code is the same with Leave Event.

Following is my code in Click Event:
Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click

       '$ 123,434,89
      Me.txtprice.Text = FormatCurrency(txtprice.Text)

 End Sub


Please help me.
Best Regards.
Posted
Updated 29-Mar-11 16:13pm
v2
Comments
Venkatesh Mookkan 29-Mar-11 22:46pm    
So, How do you want? With currency symbol or without?

1 solution

The FormatCurrency function is used in VBScript. It's only in VB.NET for backward compatibility with older code. You're also passing in a String value, not a formattable number.

Convert the text in your txtprice TextBox to a numeric type, like Single, Double, or Decimal and you can then pass it to String.Format:
Dim price As Single = Single.Parse(txtprice.Text)
txtPrice.Text = String.Format("{0:C}", price)

This is just a quick example and nowhere near what I would actually do in production quality code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900