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,
DovbermanAsked:
Who is Participating?
 
Anurag ThakurTechnical 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
 
Ken FayalCTOCommented:
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;

Open in new window

0
 
rowansmithCommented:
       string s = "32.45";
        double d = Convert.ToDouble(s.ToString());
        Int16 intFinal = Convert.ToInt16(d * 0.08);
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
rowansmithCommented:
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
 
souvik2008Commented:
I have tested it with this code.          

            string str = "32.45";
            double dub = Double.Parse(str);
            Int16 inVar = new Int16();
            inVar = (Int16) dub;
0
 
Ken FayalCTOCommented:
Woops.. have to change mine:


decimal a = 0;
string str = "32.45";
int result = 0;
 
a = decimal.Parse(str) * (decimal).8;
result = (int)a;

Open in new window

0
 
souvik2008Commented:
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
 
Hamid HassanCommented:
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
 
Tony McCreathTechnical 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);           
}

Open in new window

0
 
DovbermanAuthor 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.

All Courses

From novice to tech pro — start learning today.