Results 1 to 9 of 9

Thread: Textbox - Currency without "$"

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Textbox - Currency without "$"

    Hi, all,

    I am wanting to display currency in a TextBox, but without the "$".
    Also, if the end user types in a negative sign before the numbers, the entire amount is surrounded in ().
    I have the first part working, but I am unable to get the "$" to go away.

    Code:
    StartingBalanceAsset.Text = Convert.ToDecimal(StartingBalanceAsset.Text).ToString("C2")
    The above works without an issue.
    If I type in 123456, the value is then converted to $123,456.00
    If I type in -123456, the value is then converted to ($123,456.00)

    I tried to use the following to remove the "$" and nothing happens. No errors either.

    Code:
    StartingBalanceAsset.Text.Replace("$", String.Empty)
    Any suggestions?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Textbox - Currency without "$"

    The "C" format specifier is specifically for currency. If you don't want currency, which you don't if you don't want a currency symbol, then don't use "C". Use the format specifier that provides just the desired decimal and group (thousand) separators, which will presumably be "N" or "F".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Textbox - Currency without "$"

    I'm not sure whether "C" also inherently uses parentheses for negative values while "N" and "F" do not but, if that is the case, you can simply provide an appropriate NumberFormatInfo when calling ToString.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    New Member
    Join Date
    Dec 2014
    Posts
    7

    Re: Textbox - Currency without "$"

    Wouldn't the Format "D" Work since it would still contain the Decimal places just without the $?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Textbox - Currency without "$"

    Quote Originally Posted by Avaion View Post
    Wouldn't the Format "D" Work since it would still contain the Decimal places just without the $?
    I am not familiar with many of the formats, but i'll check into it.
    I haven't coded in a while, so getting back into the swing of things.
    Thanks for the possibility.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Textbox - Currency without "$"

    Quote Originally Posted by Avaion View Post
    Wouldn't the Format "D" Work since it would still contain the Decimal places just without the $?
    "D" is supported by integral types only and the result is an integer value with no decimal places. I know that "F" and "N" will work and I think that "G" will too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Textbox - Currency without "$"

    F2 should give you a decimal number with 2 places.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Textbox - Currency without "$"

    Sounds like you need a custom format string so that you can specify different behaviour for positive and negative numbers ( http://msdn.microsoft.com/en-us/libr...ctionSeparator ). Unfortunately, this means AFAIK that you cannot then use standard format specifiers, so you need to define each one with custom specifiers:

    vbnet Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim numbers = {1, 123, 1234, 123456.0, -1, -123, -1234, -123456.0, -123456789}
    5.  
    6.         For Each number In numbers
    7.             Console.WriteLine("{0:#,##0.00;(#,##0.00)}", number)
    8.         Next
    9.         Console.ReadLine()
    10.     End Sub
    11.  
    12. End Module

    [edit: so your code will want to look something like this:
    Code:
    StartingBalanceAsset.Text = Convert.ToDecimal(StartingBalanceAsset.Text).ToString("#,##0.00;(#,##0.00)")

  9. #9
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Textbox - Currency without "$"

    Or, having played with this a bit more... supposing you want to keep the rest of the current culture's settings... (e.g. european countries' use of "," as the decimal point, etc, which the above code does not do)

    First of all Clone the current locale's NumberFormatInfo, and override the CurrencySymbol to be blank, and CurrencyNegativePattern and CurrencyPositivePattern to be 0 (for what these values mean see here: http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx and http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx ). This will let you use the "C" standard formatter as long as you pass the cloned and altered numberformatinfo object. Because you've only overridden certain aspects, the thousands separator and so on will still match what the user has set as their preference.

    Console app for demonstration, note how the French culture comes out with spaces for the separator and "," for the decimal point:

    vbnet Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.  
    5.         Dim usCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US")
    6.         Dim ukCulture = System.Globalization.CultureInfo.GetCultureInfo("en-GB")
    7.         Dim frCulture = System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
    8.  
    9.         Dim baseCultures = {usCulture, ukCulture, frCulture}
    10.  
    11.         For Each baseCulture In baseCultures
    12.  
    13.             System.Threading.Thread.CurrentThread.CurrentCulture = baseCulture
    14.  
    15.             Dim numbers = {1, 123, 1234, 123456.0, -1, -123, -1234, -123456.0, -123456789}
    16.             Dim numberFormat = System.Globalization.NumberFormatInfo.CurrentInfo.Clone
    17.             numberFormat.CurrencySymbol = ""
    18.             numberFormat.CurrencyNegativePattern = 0
    19.             numberFormat.CurrencyPositivePattern = 0
    20.  
    21.             For Each number In numbers
    22.                 Console.WriteLine(number.ToString("C"))
    23.                 Console.WriteLine(number.ToString("C", numberFormat))
    24.             Next
    25.  
    26.         Next
    27.         Console.ReadLine()
    28.     End Sub
    29.  
    30. End Module

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