0

My question might looks like silly, but i struck with it. I have a string value "155.300" and i want to convert it to integer. I tryed but throwing System.FormatException....pls someone help me out.

2
  • What's the code you're trying to use? Commented Sep 4, 2012 at 10:32
  • Actually i am getting a string value which is a decimel number as i mentioned above and i need to multiple with 1000. so that it shld be like 155300 and hv to send it to server. Commented Sep 4, 2012 at 10:36

4 Answers 4

5

Since your source data is string you need to Convert it to Double first then just cast it to int or use Convert.ToInt32, but remember Convert.ToInt32 rounds it to nearest integer number, whereas casting takes the int part of the number (truncate)

double d = Convert.ToDouble("155.00");    
int a = (int) d;
int b = Convert.ToInt32(d);

Or in a single Line

int b =(int) Convert.ToDouble("155.000");

EDIT

Since you want to use decimal point as thousand separator, I believe in German culture you can try the following.

int b = ((int)Convert.ToDouble("154.500", new CultureInfo("de-DE")));

That will give you 154500

EDIT 2

Or much better is to use int.Parse with NumberStyles.AllowThousands:

 int b = int.Parse("154.500", NumberStyles.AllowThousands, new CultureInfo("de-DE"));
Sign up to request clarification or add additional context in comments.

13 Comments

Hi Habib, i tried both the cases not getting correct answers. when i used first one for my value "153.500" getting 154 and whn i tried 2nd one single line sol...getting 153.
@Tim, what do you want then ? what should be the expected output ?
if original value is "154.500" then i want 154500 as int.
well, in that case, you can do a remove the decimal and try to parse it to int.
@Tim: Is the "." here actually a thousands separator, or is this actually a value of just over 153, but you want to multiply by 1000? You need to be very clear about what your data means.
|
5

First parse it as a decimal or double (probably best to use decimal as you've got decimal data) then either cast or use something like Math.Round, depending on your requirements.

Basically, you need to always consider what data you've got: "155.300" isn't a string representation of an integer, so don't try to parse it as an integer. Parse it as what it is, then convert that to an integer.

Alternatively, you could hack at the string representation first, but personally I find that to be a more brittle approach in many cases.

EDIT: Note that if this is really already an integer, but with a thousands separator, you don't need to use double at all - you can use int.Parse, specifying an appropriate culture and number style:

int parsed = int.Parse(text, NumberStyles.Integer | NumberStyles.AllowThousands,
                       culture);

2 Comments

is there a method int.ParseExact ? because I can't seem to find it
@Habib +1 for finding an error in the compiler generated code
0

Here is a working conversion sample. Take a special look with the edge conditions, the output may be different if using several rounding/casting techniques

class Program
{
    public static int MyToInt(string str)
    {
        double result;
        bool success = Double.TryParse(str, out result);
        if (!success)
        {
            throw new ArgumentException(
                "Cannot parse a string into a double");
        }

        return Convert.ToInt32(result);     // 156
        //return (int)result;               // 155 <<
        //return (int)Math.Round(result);   // 156
    }

    static void Main(string[] args)
    {
        string s = "155.500";
        int value = MyToInt(s); 
    }
}

Comments

0

You can try this:

string str = "123.123";
str = str.Remove(str.IndexOf('.'), 1);
int result;
int.TryParse(str, out result);

Edit: Based on your comment, modified to multiply by thousand. Or you can just try:

string str = "123.123";
double result;
double.TryParse(str, out result);

int final = (int)(result * 1000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.