473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

string conversion

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)

Nov 21 '05 #1
8 1476
"David A. Osborn" <do********@hotmail.com> wrote in news:un%te.91194$nG6.21524@attbi_s22:
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/
Nov 21 '05 #2
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)
'***

--
Best Regards
Yanick
"David A. Osborn" <do********@hotmail.com> a écrit dans le message de
news:un%te.91194$nG6.21524@attbi_s22...
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)

Nov 21 '05 #3
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" <do********@hotmail.com> wrote in message
news:un%te.91194$nG6.21524@attbi_s22...
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)

Nov 21 '05 #4
"David A. Osborn" <do********@hotmail.com> schrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
news:#3*************@TK2MSFTNGP12.phx.gbl:
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/
Nov 21 '05 #6
Chad,

"Chad Z. Hower aka Kudzu" <cp**@hower.org> schrieb:
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.


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
news:eA**************@TK2MSFTNGP14.phx.gbl:
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.


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/
Nov 21 '05 #8
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eA****************@TK2MSFTNGP14.phx.gbl...
Chad,

"Chad Z. Hower aka Kudzu" <cp**@hower.org> schrieb:
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.


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type...
2
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I...
12
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of...
6
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.