Solved

C#, Convert.ToInt32(string) question

Posted on 2011-03-21
3
617 Views
Last Modified: 2012-06-21
  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
Comment
Question by:oneDayAtaTime
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 300 total points
ID: 35183548
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
 
LVL 10

Assisted Solution

by:peetm
peetm earned 100 total points
ID: 35183577
You could ...

i = (Int32)Convert.ToDouble(your_string);
0
 
LVL 19

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 100 total points
ID: 35183753
This is your best bet:

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now