• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

C#, Convert.ToInt32(string) question

  I've got some C# code in an inherited application that is not working. The problem seems to be that the input is a string consisting of a decimal number. The code is trying to convert the string to an integer, using the following line
Convert.ToInt32(string)

   I did some searching and it seems that this method is expecting a whole number string, and won't work with a decimal number string. Is that correct? Is there an easy fix for this? I am expecting both whole numbers and decimal numbers as input here. Thanks for the help!

0
oneDayAtaTime
Asked:
oneDayAtaTime
3 Solutions
 
wdosanjosCommented:
Yes, Convert.ToInt32(string) only works for integers.  Try Convert.ToDecimal(string) or Convert.ToDouble(string) if you expect decimal numbers.

More on the Convert class:
http://msdn.microsoft.com/en-us/library/system.convert.aspx
0
 
peetmCommented:
You could ...

i = (Int32)Convert.ToDouble(your_string);
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
This is your best bet:

                    string a = "1.1";
                    decimal d = decimal.MinValue;
                    decimal.TryParse( a, out d );
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now