# Convert numeric string to integer C#

I have a string that contains only numeric values.  ie. "32.45"

I need to convert it into a decimal.
Then I need to multiply the decimal by 0.8
Finally I need to convert the result to an integer.

psuedocode
// convert the string to a decimal
decimal decConverted = Converted ("32.45");

// multiply the decimal by 0.8
decimal decConverted = Converted(0.8 * decConverted)

convert the resulting decimal to an integer.
int16 intFinal = Converted(decConverted)

What is the proper syntax?
Thanks,
###### Who is Participating?

Technical ManagerCommented:

// convert the string to a decimal
decimal decConverted = Convert.ToDecimal ("32.45");

// multiply the decimal by 0.8;
decimal decConverted = 0.8 * decConverted;

convert the resulting decimal to an integer.
int16 intFinal = Convert.ToInt16(decConverted);
0

CTOCommented:
LIke this:

Bear in mind that you will lose everything after the decimal point when you convert it back to int.
``````decimal a = 0;
string str = "32.45";
int result = 0;

a = decimal.Parse(str) * .8;
result = (int)a;
``````
0

Commented:
string s = "32.45";
double d = Convert.ToDouble(s.ToString());
Int16 intFinal = Convert.ToInt16(d * 0.08);
0

Commented:
And if you truly want a decimal then:

string s = "32.45";
s = s + "m";
decimal d = Convert.ToDecimal(s.ToString());
Int16 intFinal = Convert.ToInt16(d * 0.08m);
0

Commented:
I have tested it with this code.

string str = "32.45";
double dub = Double.Parse(str);
Int16 inVar = new Int16();
inVar = (Int16) dub;
0

CTOCommented:
Woops.. have to change mine:

``````decimal a = 0;
string str = "32.45";
int result = 0;

a = decimal.Parse(str) * (decimal).8;
result = (int)a;
``````
0

Commented:
I forgot about the multiplication of 0.8.

use this :-

string str = "32.45";
double dub = Double.Parse(str);
dub=dub * 0.8;
Int16 inVar = new Int16();
inVar = (Int16) dub;
0

Commented:
Hi,

I Think it will done by following...

string str = "32.45";
decimal dec = Convert.ToDecimal(str);
dec = dec * Convert.ToDecimal(0.8);
int result = Convert.ToInt32(dec);
0

Technical SEO ConsultantCommented:
This is a little more robust so would indicate a more proper way to do it.
``````/// <summary>
/// Parses the input and returns 80% of it rounded to an integer
/// </summary>
/// <param name="text">A formatted number</param>
/// <param name="nearest">Should it round to the nearest integer. Otherwise, it rounds down.</param>
/// <returns>80% of the input text</returns>
public Int32 Calculate(string text, bool nearest)
{
decimal d;

if (!Decimal.TryParse(text, System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out d)) // copes with currency formatting, commas etc.
throw new FormatException("Input must be a valid Decimal: " + text);

decimal md = d * 0.8M;

if (nearest)
md = Decimal.Round(md); // round to nearest integer value

return Convert.ToInt32(md);
}
``````
0

Author Commented:
There were so many good solutions that I had a hard time distibuting points. I picked only 4 to make the points awarded at least 60. the extra 10 points went to the solution I used.
Thanks to all.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.