Solved

C#, Convert.ToInt32(string) question

Posted on 2011-03-21
3
618 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 20

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 30
Data Saving 5 56
Host asp.net pages 5 26
Exit the loop 4 40
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

23 Experts available now in Live!

Get 1:1 Help Now