string conversion

  • Thread starter Thread starter David A. Osborn
  • Start date Start date
D

David A. Osborn

I have a textbox that is getting input in the form of a percent ie.
"100.00%". How do I convert this to a double? If i do
System.Convert.ToDouble(MyVar) I get an exception. How do I strip the
percent sign off? (Also there may not be a % because the user may just
enter 100 or 100.00)
 
Hi David ! :O)

Try something like this :
'***
Dim s As String = "100.00%"
Dim d As Double = Double.Parse(s.Replace("%", ""),
CultureInfo.InvariantCulture)
Console.WriteLine(d)
'***
 
David A. Osborn said:
I have a textbox that is getting input in the form of a percent ie.
"100.00%". How do I convert this to a double? If i do
System.Convert.ToDouble(MyVar) I get an exception. How do I strip the
percent sign off? (Also there may not be a % because the user may just
enter 100 or 100.00)

decimal x = decimal.Parse(textBox.Text.Replace("%", ""));

Its C#, but you shoudl get the point.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
June 21, 2005

Imports System.Text.RegularExpressions

regex.replace(INPUT,"%","") 'Shared method

I can't quite remember the signature and the exact position of the input
part, but this is how to strip the % sign. You should be able to convert it
to double now. Hope this helps and have a great day!

--
Joseph Bittman
Microsoft Certified Application Developer

Web Site: http://71.35.110.42
Dynamic IP -- Check here for future changes
 
David A. Osborn said:
I have a textbox that is getting input in the form of a percent ie.
"100.00%". How do I convert this to a double?

Why don't you place a label with the caption "%" next to the textbox and let
the user enter the number without the percent character?
 
Herfried K. Wagner said:
Why don't you place a label with the caption "%" next to the textbox
and let the user enter the number without the percent character?

I like that idea, but then he'll need to catch keypresses and disallow the % as users will still
enter (or try to) it.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
Chad,

Chad Z. Hower aka Kudzu said:
I like that idea, but then he'll need to catch keypresses and disallow the
% as users will still
enter (or try to) it.

Mhm... I'd analyze the input, maybe using 'Double.TryParse' and then set an
error provider for invalid input.
 
Herfried K. Wagner said:
Mhm... I'd analyze the input, maybe using 'Double.TryParse' and then
set an error provider for invalid input.

All depends what he wants to do. If the % is data that doesnt change the input, and the user might
enter it, Id just ignore it whether from the keypress or after as he asked about. Other data, yes
definitely handle or ignore, depending on the application.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
June 22, 2005

LOL Just make it easy and strip the % if there is one..... and if there
isn't it still won't cause an exception, but it will still work which is the
big thing.... lol thunder storm here....

--
Joseph Bittman
Microsoft Certified Application Developer

Web Site: http://71.35.110.42
Dynamic IP -- Check here for future changes
 
David,

I saw only javascript translated code.

:-)))

I would go for the solution from Herfried, however in real VBNet using one
of the extremely strong and fast conversion methods which are not in the
standard system.net namespace.

Dim result As Double = CDbl("100,00%".Replace("%", ""))

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top