Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
hi how to convert string to integer?
 
my string was like this "txtstring.text"
 
i want to convert "txtstring.text" to int64.
txtstring.text having the value like this 1:00:51:231 like this
i want to convert this time (1:00:51:231) to in milliseconds.
Posted 30-May-12 22:34pm
Edited 30-May-12 23:49pm
Prosan8.5K
v3
Comments
Rajesh Kariyavula at 31-May-12 4:49am
   
Is that a time that you want to convert.
sandeep nagabhairava at 31-May-12 4:55am
   
ya rajesh. time which is in string format,i want convert that to int form ... thanks for past answer..
Rajesh Kariyavula at 31-May-12 5:00am
   
Sandeep, why do you want to convert time to an integer, is it for the previous calculation for getting amount based on time??
sandeep nagabhairava at 31-May-12 5:26am
   
yes.. exactly rajesh...
Rajesh Kariyavula at 31-May-12 5:48am
   
You dont need to convert time to an integer for this, I have solved this for you, please check my answer.
Rajesh Kariyavula at 31-May-12 6:21am
   
Let me know if that doesn't suffice your need.
sandeep nagabhairava at 31-May-12 6:39am
   
thank you for caring rajesh. i solved my self...
Rajesh Kariyavula at 31-May-12 13:57pm
   
thats great Sandeep
sandeep nagabhairava at 31-May-12 14:00pm
   
thank u...:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I assume that you are talking about taking the contents of txtString.Text and converting it to an integer. There are a few ways that you can achieve this, but the way that's usually recommended is to use the TryParse method of Int64. This is what it would look like:
public static Int64 GetValue(this string value)
{
  if (string.IsNullOrWhiteSpace(value))
  {
    return 0;
  }
  Int64 output;
  // TryParse returns a boolean to indicate whether or not
  // the parse succeeded. If it didn't, you may want to take
  // some action here.
  Int64.TryParse(value, out output);
   return output;
}
  Permalink  
Comments
VJ Reddy at 31-May-12 4:59am
   
Good point to use TryParse. 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

It seems from the question that the Text value contains : characters and the numeric value is required by eliminating these characters. If that is the case then the following code may be useful
long numValue;
string text ="1:00:51:231";
Int64.TryParse(text.Replace(":",""),out numValue);
 
[Edit]
 
As seen from the comment of OP to another solution the text
1:00:51:231
represents time. In that case the string can be converted into milli seconds to generate a long number as shown below:
string text = "1:00:51:231";
long[] multipliers = new long[] {3600000,60000,1000,1};
int i=0;
var longTime = text.Split(':').Aggregate (0,(long total, string part) =>
    total += Int64.Parse(part) * multipliers[i++]);
//longTime = 3651231
  Permalink  
v3
Comments
Pete O'Hanlon at 31-May-12 5:18am
   
I like this solution, but I'm not sure the OP will be able to understand it. Gets my 5 though.
VJ Reddy at 31-May-12 5:22am
   
Thank you, Pete :)
losmac at 31-May-12 9:51am
   
No comment, just BIG 5!
VJ Reddy at 31-May-12 10:39am
   
Thank you, losmac :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

OK, seeing that your question changed from the version we originally answered, here's my take.
 
"txtstring.text having the value like this 1:00:51:231 like this"
 
First of all, you should name things better. There is no way to tell, from txtstring, what it's intended for. This is why no answer thought it was the time component.
 
Second, you state this is a time component. OK, why are you wanting to change it into a number? As it stands, time components generally make little sense if they are not coupled with a date. If you had the date and time, you could use DateTime.TryParseExact on this and extract the Tick count.
 
Third, if you are only interested in the time part, there's no real need to store it in an Int64. You can simply retrieve the milliseconds from midnight, which is an integer. Here's a quick hacky version that would do this:
private static int Parse(string time)
{
    DateTime date = DateTime.Parse("2000-01-01 00:00:00.000");
    DateTime myDate;
    if (!DateTime.TryParse(string.Format("2000-01-01 {0}", time), out myDate))
    {
        return 0;
    }
    return myDate.Subtract(date).Milliseconds;
}
In future, try to give people ALL the information they need before they answer.
 
[Edit]Just rereading your question - the time component is not a valid time component. General representation of time is Hours:Minutes:Seconds.Milliseconds; Note that there are only 2 colons in there and a period, not three colons.
  Permalink  
v2
Comments
VJ Reddy at 31-May-12 5:18am
   
Very good answer. 5!
sandeep nagabhairava at 31-May-12 5:45am
   
hi pete.
actually
my requirement was like this totalAmount=(txtstring.text/minHours)*cost.
 
hear txtstring.text(string) n value like this 1:00:51:231(it was dif betwwen two times),
minHours(int64) is some X hours(ex:3 hrs),
Cost(int64) is some Y$ (ex:4$),
i wana calculate amount in int format.
Pete O'Hanlon at 31-May-12 5:55am
   
So why didn't you state this? You cannot get a valid answer if you don't give us the real problem. This is your third attempt at the problem, which we could have given you an answer to straight off rather than you wasting our time.
Rahul Rajat Singh at 31-May-12 6:07am
   
Best answer
Pete O'Hanlon at 31-May-12 6:53am
   
Thanks. I appreciate the thought.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

You don't need to convert the time to an integer for doing that, all you will be needing is hours.
 
string [] timeDuration = txtstring.Text.Split(':');
 
if (Convert.ToInt32(timeDuration[3]) > 0)//Check if milliseconds greater than 0
{
    //If greater than 0 then increment seconds.
    timeDuration[2] = (Convert.ToInt32(timeDuration[2]) + 1).ToString();
}
if (Convert.ToInt32(timeDuration[2]) > 0)//Check if seconds greater than 0
{
    //If greater than 0 then increment minutes.
    timeDuration[1] = (Convert.ToInt32(timeDuration[1]) + 1).ToString();
}
if (Convert.ToInt32(timeDuration[1]) > 0)//Check if minutes greater than 0
{
    //If greater than 0 then increment Hours.
    timeDuration[0] = (Convert.ToInt32(timeDuration[0]) + 1).ToString();
}
Int32 Hours = Convert.ToInt32(timeDuration[0]);
 
Finally you will have Hours as integer for getting the amount.
 
Hope it helps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
sandeep nagabhairava at 31-May-12 4:42am
   
it's not working, my string was like this txtstring.text having the value like this 1:00:51:231.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

I know this post has been answered already, but for those who are looking for benchmarks, he benchmarks several different ways to convert a string to an int:
 
http://cc.davelozinski.com/c-sharp/fastest-way-to-convert-a-string-to-an-int
 
What's interesting is the fastest way isn't using any of the native C# methods Convert.ToInt32(), int.TryParse(), or int.Parse().
 
Definitely worth a read for those that are curious.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello,
try following-
static public string makeNumeric( string str, bool decimals )
  {
         bool hasdec = false;
         string ret = "";
         for(int i=0; i<= str.Length-1; i++)
         {
    if(Char.IsDigit(str, i))
    {
         ret += str[i];
    }
    else if( decimals && str[i] == ':' && !hasdec )
    {
         ret += str[i];
         hasdec = true;
    }
         }
         return ret;
  }
 
call this function and assign the result to Integer variable. if any problem arises ask me.
  Permalink  
v3
Comments
sandeep nagabhairava at 31-May-12 4:45am
   
it's not working, my string was like this txtstring.text having the value like this 1:00:51:231.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Every numeric type in .Net has Parse and TryParse methods.
Use them for conversion of numbers represented as strings to their numeric values.
 
long myNum = long.Parse(txtstring.text);
 
or 
 
long.TryParse(txtstring.text, out myNum );
  Permalink  
Comments
sandeep nagabhairava at 31-May-12 4:45am
   
it's not working, my string was like this txtstring.text having the value like this 1:00:51:231.
Pete O'Hanlon at 31-May-12 4:49am
   
That's not a valid number, you can't parse that as it stands. You'd have to remove the : values.
sandeep nagabhairava at 31-May-12 4:51am
   
it's not a number, it's time. how can we convert it into int form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 335
1 Nirav Prabtani 252
2 Richard Deeming 215
3 CHill60 170
4 _Amy 145
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,045
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 5 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100