简体   繁体   中英

converting double/floating to integer in C#

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.

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"));

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);

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); 
    }
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM