none
Convert Number as string to Integer RRS feed

  • Question

  • Hi,

    I have a listbox with items selected from a list which holds numbers with decimal points. I should convert this to Integer

     

    I tried using Convert.ToInt32 but it is giving error as it is expecting only whole numbers.

     

    How to convert the decimal numbered string to a proper Integer...? Please help

     

     

    Monday, December 6, 2010 8:28 AM

Answers

  • Hi Gai3kannan,

     

                    Sorry to keep you waiting.

                    I have written a function for your issue, hope this could help you out.

                         //SPList list = site.Lists["www"];

               //SPListItem item = list.Items[0];

               //int price=ConvertToInt(item["price"].ToString());

                            private static int ConvertToInt(string price)

            {

                string str = price.Split('$').Last();

                decimal  strDec = Convert.ToDecimal(str);

                int strInt = Decimal.ToInt32(strDec);

                return strInt;

            }

                   

     

    Best Regards,

    Porter Wang

    • Proposed as answer by Daniel Dragnea Tuesday, December 14, 2010 11:29 PM
    • Unproposed as answer by Gai3kannan Thursday, December 16, 2010 7:05 AM
    • Marked as answer by Gai3kannan Thursday, December 16, 2010 7:05 AM
    Tuesday, December 14, 2010 2:35 AM

All replies

  • Sorry, I am not looking for rounding functions... instead need to use items(string) from a listbox which could be like $2.50, $21, $400.50. I need to convert this to integer to sum up. when i do for $21 it is working fine, but when i convert $400.50 or $2.50 it gives InvalidCast Exception error as when converting from string to Integer the numbers are expected to be whole numbers.

    How to solve this..?

    Wednesday, December 8, 2010 6:36 AM
  • Hi Gai3kannan,

     

                    Sorry to keep you waiting.

                    I have written a function for your issue, hope this could help you out.

                         //SPList list = site.Lists["www"];

               //SPListItem item = list.Items[0];

               //int price=ConvertToInt(item["price"].ToString());

                            private static int ConvertToInt(string price)

            {

                string str = price.Split('$').Last();

                decimal  strDec = Convert.ToDecimal(str);

                int strInt = Decimal.ToInt32(strDec);

                return strInt;

            }

                   

     

    Best Regards,

    Porter Wang

    • Proposed as answer by Daniel Dragnea Tuesday, December 14, 2010 11:29 PM
    • Unproposed as answer by Gai3kannan Thursday, December 16, 2010 7:05 AM
    • Marked as answer by Gai3kannan Thursday, December 16, 2010 7:05 AM
    Tuesday, December 14, 2010 2:35 AM
  • Thanks Porter Wang...Yours really helped me to solve my issue. Hats off to you :)
    Thursday, December 16, 2010 7:05 AM