Textbox: Convert to float and backwards
-
Hi guys! My question is certainly trivial for you... :sigh: I want to convert the text of a textbox to a float with two digits after the separator. And then the whole thing backwards: Convert the float to a string, always showing two digits after the separator. Right now I do it like that:
float myFloat = float.Parse(TextBox1.Text); // string --> float
TextBox1.Text = myFloat.ToString(); // float --> stringEverything works fine except for trailing zeros (e.g. '12,30' or 123,00'). The textbox doesn't show any trailing zeros. (I use a comma as separator cause it's a german application) What can I do to show trailing zeros? Do I have to use IFormatProvider in some way? Btw... these variables represent amounts of money (€). Is it possible to set somthing like a "currency-mode" for the textbox? Many thanks in advance! :rose: mYkel
-
Hi guys! My question is certainly trivial for you... :sigh: I want to convert the text of a textbox to a float with two digits after the separator. And then the whole thing backwards: Convert the float to a string, always showing two digits after the separator. Right now I do it like that:
float myFloat = float.Parse(TextBox1.Text); // string --> float
TextBox1.Text = myFloat.ToString(); // float --> stringEverything works fine except for trailing zeros (e.g. '12,30' or 123,00'). The textbox doesn't show any trailing zeros. (I use a comma as separator cause it's a german application) What can I do to show trailing zeros? Do I have to use IFormatProvider in some way? Btw... these variables represent amounts of money (€). Is it possible to set somthing like a "currency-mode" for the textbox? Many thanks in advance! :rose: mYkel
TextBox1.Text = myFloat.ToString("F");
Charlie if(!curlies){ return; } -
Hi guys! My question is certainly trivial for you... :sigh: I want to convert the text of a textbox to a float with two digits after the separator. And then the whole thing backwards: Convert the float to a string, always showing two digits after the separator. Right now I do it like that:
float myFloat = float.Parse(TextBox1.Text); // string --> float
TextBox1.Text = myFloat.ToString(); // float --> stringEverything works fine except for trailing zeros (e.g. '12,30' or 123,00'). The textbox doesn't show any trailing zeros. (I use a comma as separator cause it's a german application) What can I do to show trailing zeros? Do I have to use IFormatProvider in some way? Btw... these variables represent amounts of money (€). Is it possible to set somthing like a "currency-mode" for the textbox? Many thanks in advance! :rose: mYkel
TextBox1.Text = myFloat.ToString("c"); check out Chris Sells format designer to experiment with format strings : http://www.sellsbrothers.com/tools/#FormatDesigner
The smaller the mind the greater the conceit. Aesop
-
TextBox1.Text = myFloat.ToString("c"); check out Chris Sells format designer to experiment with format strings : http://www.sellsbrothers.com/tools/#FormatDesigner
The smaller the mind the greater the conceit. Aesop
"C" and "c" are the format specifiers for currency, so unless you want a currency symbol in your number, don't use it. Instead, use "F" or "f" instead.
Microsoft MVP, Visual C# My Articles
-
"C" and "c" are the format specifiers for currency, so unless you want a currency symbol in your number, don't use it. Instead, use "F" or "f" instead.
Microsoft MVP, Visual C# My Articles
Good point, he did say 'Is it possible to set somthing like a "currency-mode" for the textbox?' though so I though thats what he wanted. Cheers.
The smaller the mind the greater the conceit. Aesop
-
Good point, he did say 'Is it possible to set somthing like a "currency-mode" for the textbox?' though so I though thats what he wanted. Cheers.
The smaller the mind the greater the conceit. Aesop
You're absolutely right, sorry! A lot of sentences in there and I guess I missed the last one. :-O In any case - to the original poster if you're following along - you should look at the
CultureInfo
class and its related properties (IFormatProvider
implementations). For mostToString
overloads (not the override fromObject
or another base class), theIFormatProvider
(like theNumberFormatInfo
) is used for theThread.CurrentUICulture
, so you don't really need to specify one unless you want to format for a different culture (just for that instance, otherwise you should setThread.CurrentUICulture
and re-initialize your controls) or pass your ownIFormatProvider
. Typically, such aToString
method (or methods likeString.Format
) will specify if they use the currentCultureInfo
for the format specifier.Microsoft MVP, Visual C# My Articles
-
You're absolutely right, sorry! A lot of sentences in there and I guess I missed the last one. :-O In any case - to the original poster if you're following along - you should look at the
CultureInfo
class and its related properties (IFormatProvider
implementations). For mostToString
overloads (not the override fromObject
or another base class), theIFormatProvider
(like theNumberFormatInfo
) is used for theThread.CurrentUICulture
, so you don't really need to specify one unless you want to format for a different culture (just for that instance, otherwise you should setThread.CurrentUICulture
and re-initialize your controls) or pass your ownIFormatProvider
. Typically, such aToString
method (or methods likeString.Format
) will specify if they use the currentCultureInfo
for the format specifier.Microsoft MVP, Visual C# My Articles
Thanks so much for your numerous and quick answers! I knew there is a simple solution... ;) The
myFloat.ToString("F")
does all I need. There's no difference between "f" and "F", right? Regards, mYkel -
Thanks so much for your numerous and quick answers! I knew there is a simple solution... ;) The
myFloat.ToString("F")
does all I need. There's no difference between "f" and "F", right? Regards, mYkelMykel wrote: There's no difference between "f" and "F", right? Nope. See the documentation for
NumberFormatInfo
in the .NET Framework SDK for more information.Microsoft MVP, Visual C# My Articles