|
-
Jan 4th, 2015, 01:58 AM
#1
Thread Starter
PowerPoster
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?
-
Jan 4th, 2015, 02:06 AM
#2
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".
-
Jan 4th, 2015, 02:09 AM
#3
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.
-
Jan 8th, 2015, 10:40 AM
#4
New Member
Re: Textbox - Currency without "$"
Wouldn't the Format "D" Work since it would still contain the Decimal places just without the $?
-
Jan 8th, 2015, 10:57 AM
#5
Thread Starter
PowerPoster
Re: Textbox - Currency without "$"
 Originally Posted by Avaion
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.
-
Jan 8th, 2015, 11:54 AM
#6
Re: Textbox - Currency without "$"
 Originally Posted by Avaion
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.
-
Jan 8th, 2015, 11:56 AM
#7
Re: Textbox - Currency without "$"
F2 should give you a decimal number with 2 places.
-tg
-
Jan 8th, 2015, 11:59 AM
#8
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:
Module Module1
Sub Main()
Dim numbers = {1, 123, 1234, 123456.0, -1, -123, -1234, -123456.0, -123456789}
For Each number In numbers
Console.WriteLine("{0:#,##0.00;(#,##0.00)}", number)
Next
Console.ReadLine()
End Sub
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)")
-
Jan 8th, 2015, 12:24 PM
#9
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:
Module Module1 Sub Main() Dim usCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US") Dim ukCulture = System.Globalization.CultureInfo.GetCultureInfo("en-GB") Dim frCulture = System.Globalization.CultureInfo.GetCultureInfo("fr-FR") Dim baseCultures = {usCulture, ukCulture, frCulture} For Each baseCulture In baseCultures System.Threading.Thread.CurrentThread.CurrentCulture = baseCulture Dim numbers = {1, 123, 1234, 123456.0, -1, -123, -1234, -123456.0, -123456789} Dim numberFormat = System.Globalization.NumberFormatInfo.CurrentInfo.Clone numberFormat.CurrencySymbol = "" numberFormat.CurrencyNegativePattern = 0 numberFormat.CurrencyPositivePattern = 0 For Each number In numbers Console.WriteLine(number.ToString("C")) Console.WriteLine(number.ToString("C", numberFormat)) Next Next Console.ReadLine() End Sub 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|