Results 1 to 10 of 10

Thread: [RESOLVED] Visual studio 2017 how can i display a currency symbol c#?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,144

    Resolved [RESOLVED] Visual studio 2017 how can i display a currency symbol c#?

    how can i display a currency symbol in this line of code
    Code:
    Item.SubItems.Add(Reader["CarPrice"].ToString());
    regards
    salsa

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: Visual studio 2017 how can i display a currency symbol c#?

    If this is a C# question then it should be posted in the C# forum. Polluting the VB.NET forum with C# questions because you think you might get more views is abusing the site. I have asked the mods to move this thread to the appropriate forum.

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,524

    Re: Visual studio 2017 how can i display a currency symbol c#?

    Quote Originally Posted by salsa31 View Post
    how can i display a currency symbol in this line of code
    Code:
    Item.SubItems.Add(Reader["CarPrice"].ToString());
    regards
    salsa
    you don't
    add the Format in your Sql, it is a Database ?

    this will work with a Access DB, checkout the others (Msql etc...) for the Format

    Code:
      Dim sSQL As String = "Select  OrderID, CustomerID, OrderDate, Format(Freight,'Currency')As myFreight From Orders 
    'load the Listview..
    '......
    
    "
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: Visual studio 2017 how can i display a currency symbol c#?

    Quote Originally Posted by ChrisE View Post
    you don't
    add the Format in your Sql, it is a Database ?

    this will work with a Access DB, checkout the others (Msql etc...) for the Format

    Code:
      Dim sSQL As String = "Select  OrderID, CustomerID, OrderDate, Format(Freight,'Currency')As myFreight From Orders 
    'load the Listview..
    '......
    
    "
    That's probably not the best option. If you do that then the result set you get back will contain text rather than numbers. That may not be an issue in some cases but it means that no operations that require numbers, e.g. arithmetic, can be performed on the data. It would generally be preferable to retrieve the numeric data and then format it for display in the application, as has been requested. Doing so is very simple but I won't be providing a solution as long as this thread is in the wrong forum, because doing so would only incentivise people to post questions in the wrong forum again in future.

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,524

    Re: Visual studio 2017 how can i display a currency symbol c#?

    Oh,

    now I see it's for the C#

    That's probably not the best option. If you do that then the result set you get back will contain text rather than numbers
    I don't think it will be an issue

    this will sum up correctly
    Code:
      Dim TotalSum As Decimal = 0
    
            Dim TempNode As ListViewItem
            For Each TempNode In ListView1.Items
                TotalSum += CDec(TempNode.SubItems.Item(3).Text)
            Next
            MsgBox(TotalSum)
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,579

    Re: Visual studio 2017 how can i display a currency symbol c#?

    Moved to C# forum

    I'm with JM on this one. Presentation should generally be handled in the presentation layer. The OP hasn't said how the value is actually being displayed but I'm guessing it's in something like a grid, list view or similar. It should be possible to set the format property of the column.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: Visual studio 2017 how can i display a currency symbol c#?

    Given the use of Item.SubItems, I'm guessing that this is a WinForms ListView. Many, MANY people use a ListView when they should be using a DataGridView. In WinForms, if you're not using the grouping feature and you're only displaying Details view, there's almost no justification for using a ListView over a DataGridView. Unlike a ListView, a DataGridView supports data-binding, so there's no repetitive code to load the data. The grid can also handle formatting for you. In this case, it would be a case of setting the DefaultCellStyle.Format property of the appropriate column to the appropriate format specifier. That would be "c" for the default local currency format, or you can use something like "c2" to force the number of decimal places. If the formatting must be done in code, as is the case with a ListView, then you still use the same format specifier, but in a slightly different way. This won't work:
    csharp Code:
    1. Item.SubItems.Add(Reader["CarPrice"].ToString("c"));
    because there is no such overload of ToString on the Object type. You need to either cast/convert to type Decimal first, e.g.
    csharp Code:
    1. Item.SubItems.Add(((decimal) Reader["CarPrice"]).ToString("c"));
    or:
    csharp Code:
    1. Item.SubItems.Add(Reader.GetDecimal(Reader.GetOrdinal("CarPrice")).ToString("c"));
    or else not use ToString, e.g.
    csharp Code:
    1. Item.SubItems.Add(string.Format("{0:c}", Reader["CarPrice"]));
    or:
    csharp Code:
    1. Item.SubItems.Add($"{Reader["CarPrice"]:c}");

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,171

    Re: Visual studio 2017 how can i display a currency symbol c#?

    Quote Originally Posted by ChrisE View Post
    Oh,

    now I see it's for the C#


    I don't think it will be an issue

    this will sum up correctly
    Code:
      Dim TotalSum As Decimal = 0
    
            Dim TempNode As ListViewItem
            For Each TempNode In ListView1.Items
                TotalSum += CDec(TempNode.SubItems.Item(3).Text)
            Next
            MsgBox(TotalSum)
    But why would you convert numbers to text and then back to numbers when you can just use the original numbers? Also, why would you loop through a control when you can use the Compute method of a DataTable, which would not work if the data was text? Many things are possible, but that doesn't mean that they should be done.

    That said, the OP is using a data reader in this case, so that does tend to imply that they don't want to do anything with the data other than display it. That's certainly not guaranteed though.

  9. #9

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,144

    Re: Visual studio 2017 how can i display a currency symbol c#?

    JMC BINGO!!
    Code:
    Item.SubItems.Add(((decimal) Reader["CarPrice"]).ToString("c"));

  10. #10
    Lively Member
    Join Date
    Jan 2020
    Posts
    90

    Re: [RESOLVED] Visual studio 2017 how can i display a currency symbol c#?

    Hello, @salsa31

    Please try this code,To display a currency symbol of C# in Visual studio 2017:

    Code:
     
     double value = 234.66;
           Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));
          Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
    I hope above code will be useful for you.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width