Solved

How do I format a string to display as currency?

Posted on 2008-06-18
7
737 Views
Last Modified: 2010-08-05
Hi, in my application I have a string that I would like to format to display as currency in a text box. I'm setting a variable to a Session on one form and passing it to the next, which is where the text box is located. Is there a way I can do this? I've tried this:

Session("Tuition") = String.Format("{0:C}", vBasicTuition)

but it doesn't seem to work. This is on the first form, and I'm just setting the text box to equal the Session on the second form.  I would really appreciate any help.
0
Comment
Question by:savache27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 19

Accepted Solution

by:
Stephen Manderson earned 500 total points
ID: 21814954
Hi there

The following will convert a number string to currency

        Dim vBasicTuition As Integer = Convert.ToInt32("98064")
        Dim s As String = vBasicTuition.ToString("C0", New CultureInfo("en-US"))

Regards
Steve
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21814996
Sorry forgot to include CultureInfo requires you to import

Imports System.Globalization

Also I would convert.todecimal not int32 :-)

0
 

Author Comment

by:savache27
ID: 21815754
I'm sorry, how can I use this to make the vBasicTuition format as currency. The way you gave it to me worked........but it returned $98, 064. I'm sure you were just using that as an example, but what do I replace that with? I'm including my code for the sub.
Private Sub FindTuition()
 
        Dim OffCampusNotParents(4) As String
        Dim OnCampus(4) As String
        Dim OffCampusWParents(4) As String
        OffCampusNotParents(0) = 3262  '<1/2 time
        OffCampusNotParents(1) = 14644  '1/2 time
        OffCampusNotParents(2) = 15344 '3/4 time
        OffCampusNotParents(3) = 15983 'Full Time
 
        OnCampus(0) = 3262  '<1/2 time
        OnCampus(1) = 12612 '1/2 time
        OnCampus(2) = 13312 '3/4 time
        OnCampus(3) = 13951 'Full Time
 
        OffCampusWParents(0) = 3262  '<1/2 time
        OffCampusWParents(1) = 9610  '1/2 time
        OffCampusWParents(2) = 10310 '3/4 time
        OffCampusWParents(3) = 10949 'Full Time
 
        Dim vBasicTuition As Integer = Convert.ToDecimal("98064")
        Dim s As String = vBasicTuition.ToString("C0", New CultureInfo("en-US"))
        Dim vNonResFee, vClassification, vSemesters
        vClassification = ddHours.SelectedValue
        vNonResFee = 6078 ' amount to add for non-residents
 
   
        If ddHousing.SelectedValue = 1 Then '1 = OnCampus, 2 = OffCampus With Parents, 3 = OffCampus W/O Parents
 
            vBasicTuition = OnCampus(vClassification)
 
        ElseIf ddHousing.SelectedValue = 2 Then
 
            vBasicTuition = OffCampusWParents(vClassification)
        Else
            vBasicTuition = OffCampusNotParents(vClassification)
 
        End If
 
        If ddResident.SelectedValue = 3 Then 'value of 1 or 2 = do not add, 3 = do add non-resident fee
 
            vBasicTuition = vBasicTuition + vNonResFee
        Else
            vBasicTuition = vBasicTuition
        End If
       
        Session("Tuition") = s
        Response.Redirect("estimated_cost.aspx")
 
End Sub

Open in new window

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21816056
You need to pass the value you wish to convert from string to currency into the convert.todecimal method.

This should do you
        Dim OffCampusNotParents(4) As String
        Dim OnCampus(4) As String
        Dim OffCampusWParents(4) As String
        OffCampusNotParents(0) = 3262  '<1/2 time
        OffCampusNotParents(1) = 14644  '1/2 time
        OffCampusNotParents(2) = 15344 '3/4 time
        OffCampusNotParents(3) = 15983 'Full Time
 
        OnCampus(0) = 3262  '<1/2 time
        OnCampus(1) = 12612 '1/2 time
        OnCampus(2) = 13312 '3/4 time
        OnCampus(3) = 13951 'Full Time
 
        OffCampusWParents(0) = 3262  '<1/2 time
        OffCampusWParents(1) = 9610  '1/2 time
        OffCampusWParents(2) = 10310 '3/4 time
        OffCampusWParents(3) = 10949 'Full Time
 
        Dim StrToCurrency as Integer
        Dim vNonResFee, vClassification, vSemesters
        vClassification = ddHours.SelectedValue
        vNonResFee = 6078 ' amount to add for non-residents
 
   
        If ddHousing.SelectedValue = 1 Then '1 = OnCampus, 2 = OffCampus With Parents, 3 = OffCampus W/O Parents
 
            vBasicTuition = OnCampus(vClassification)
 
        ElseIf ddHousing.SelectedValue = 2 Then
 
            vBasicTuition = OffCampusWParents(vClassification)
        Else
            vBasicTuition = OffCampusNotParents(vClassification)
 
        End If
 
        If ddResident.SelectedValue = 3 Then 'value of 1 or 2 = do not add, 3 = do add non-resident fee
 
            vBasicTuition = vBasicTuition + vNonResFee
        Else
            vBasicTuition = vBasicTuition
        End If
 
        StrToCurrency = Convert.ToDecimal(vBasicTuition)
       
        Session("Tuition") = StrToCurrency.ToString("C0", New CultureInfo("en-US"))

Open in new window

0
 

Author Comment

by:savache27
ID: 21816069
Never mind, I got it to work using the following code:

Dim vBasicTuition As Integer
Dim s As String
s = String.Format("{0:c}", vBasicTuition)
Session("Tuition") = s

I didn't use your code, but what you gave me worked and helped me to figure out a way to do it. I really appreciate the help!
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21816070
Add in to your declarations

Dim vBasicTuition as String
0
 

Author Closing Comment

by:savache27
ID: 31468448
Thanks again!
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question