I am needing some help converting a string to a float value. The string could be in any of the following formats: x x.y x/y. Obviously, I am having issues w/ the fraction string. The other formats are very easy. I have searched other places online but I cannot find anything on the fraction format. Any suggestions for this fairly n00b would be appreciated.
3 1357
I am needing some help converting a string to a float value. The string could be in any of the following formats: x x.y x/y. Obviously, I am having issues w/ the fraction string. The other formats are very easy. I have searched other places online but I cannot find anything on the fraction format. Any suggestions for this fairly n00b would be appreciated.
Well if you are going to take those three formats, you should first ensure that your string is of a proper format. Once there, you need to do two different things. If its one of the first two formats, just parse it. If its the last, you need to split it by the / and then check each of the two numbers, which I assume, each can be a form of the first two (hence, you can have x.y/z or even a.b/x.y).
Here is some sample code: -
[STAThread]
-
static void Main(string[] args)
-
{
-
Console.WriteLine(ParseStringForDoubleExpression("5.3"));
-
Console.WriteLine(ParseStringForDoubleExpression("7"));
-
Console.WriteLine(ParseStringForDoubleExpression("4/5.4"));
-
try
-
{
-
Console.WriteLine(ParseStringForDoubleExpression("4/5/3"));
-
}
-
catch
-
{
-
Console.WriteLine("Exception handled");
-
}
-
Console.ReadKey(true);
-
}
-
-
private static double ParseStringForDoubleExpression(string value)
-
{
-
double result = 0.0;
-
if (string.IsNullOrEmpty(value))
-
throw new NullReferenceException("Parameter 'value' cannot be null or empty");
-
-
if (value.Contains("/"))
-
{
-
result = ParseForDivision(value);
-
}
-
else
-
{
-
result = GetDoubleValue(value);
-
}
-
-
return result;
-
}
-
-
private static double ParseForDivision(string value)
-
{
-
if (value.Contains("/"))
-
{
-
if (value.Split('/').Length != 2)
-
throw new Exception("Parameter 'value' is of incorrect format. Acceptable formats are: X or X.Y or X/Y");
-
}
-
else
-
{
-
throw new Exception("Parameter 'value' is of incorrect format. Acceptable formats are: X or X.Y or X/Y");
-
}
-
-
double firstResult = GetDoubleValue(value.Split('/')[0]);
-
double secondResult = GetDoubleValue(value.Split('/')[1]);
-
-
return firstResult / secondResult;
-
}
-
-
private static double GetDoubleValue(string value)
-
{
-
long placeHolder;
-
double result;
-
if (CheckForLong(value, out placeHolder))
-
{
-
result = placeHolder;
-
}
-
else if (CheckForDouble(value, out result))
-
{ }
-
else
-
{
-
throw new Exception("Parameter 'value' is of incorrect format. Acceptable formats are: X or X.Y or X/Y");
-
}
-
-
return result;
-
}
-
-
private static bool CheckForDouble(string value, out double result)
-
{
-
if (double.TryParse(value, out result))
-
{
-
if (value == result.ToString())
-
{
-
return true;
-
}
-
}
-
-
return false;
-
}
-
-
private static bool CheckForLong(string value, out long result)
-
{
-
if (long.TryParse(value, out result))
-
{
-
if (value == result.ToString())
-
{
-
return true;
-
}
-
}
-
-
return false;
-
}
-
I should have clarified previously, but I am developing in C# using Visual Studio 2003. There isn't oString.contains method, which is the tricky part. I already tried using that. I do appreciate the help though. If there are any other suggestions I will gladly take them.
I should have clarified previously, but I am developing in C# using Visual Studio 2003. There isn't oString.contains method, which is the tricky part. I already tried using that. I do appreciate the help though. If there are any other suggestions I will gladly take them.
Use IndexOf instead then. If it returns -1, it doesnt exist. If it returns a value, split to that value, and check from that value on. If it isnt -1, '/' appears more then once.
Example: -
string myString = "whatever";
-
if(myString.IndexOf('/') != -1)
-
{
-
if(myString.Substring(myString.IndexOf('/'), myString.Length - myString.IndexOf('/') - 1) != -1)
-
{ // too many '/' }
-
else
-
{ // right amount
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Cally |
last post by:
Hello,
I would like to convert a field from ntext field found in one database
table to float field found in another database table. The reason why I
want to do this is a long one.
I have...
|
by: jaijai_kumar |
last post by:
Select Cast('100.1234' as float)
give me the result 100.1234
Now when I convert it back to char I want exactly 100.1234
Select Convert(char(100),Cast('100.1234' as float))
Gives me 100.123 (Here...
|
by: aevans1108 |
last post by:
expanding this message to microsoft.public.dotnet.xml
Greetings
Please direct me to the right group if this is an inappropriate place
to post this question. Thanks.
I want to format a...
|
by: Chi Tang |
last post by:
Hi,
I try to convert a string to a float but it alway comes out with extra
value. For example, the string input is '12.6' but the output is
'12.6000003814697' The following is my code to do...
|
by: shellon |
last post by:
Hi all:
I want to convert the float number to sortable integer, like the
function float2rawInt() in java, but I don't know the internal
expression of float, appreciate your help!
|
by: lisasahu |
last post by:
how to convert float to string like there is atof atoi and itoa but is ther any method to convert float to string
|
by: jeff_rowa |
last post by:
Hi guyz,
Could anyone please tell me how can I convert an array of floats to a string? I know that I can use this code to convert one float to a string but what is the best way for an array of...
|
by: tereglow |
last post by:
Hello,
I am a complete newbie to Python and am accustomed to coding in PHP/
Perl/Shell. I am trying to do the following:
I have a string:
cpuSpeed = 'Speed: 1000000000'
What I would...
|
by: Peter |
last post by:
Trying to convert string to byte array.
the following code returns byte array of {107, 62, 194, 139, 64}
how can I convert this string to a byte array of {107, 62, 139, 65}
...
|
by: Marco Nef |
last post by:
Hi there
I'm looking for a template class that converts the template argument to a
string, so something like the following should work:
Convert<float>::Get() == "float";
Convert<3>::Get() ==...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |