Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1361
  • Last Modified:

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,
0
Dovberman
Asked:
Dovberman
  • 2
  • 2
  • 2
  • +4
4 Solutions
 
Ken FayalCommented:
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
 
Anurag ThakurCommented:

// 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
 
rowansmithCommented:
       string s = "32.45";
        double d = Convert.ToDouble(s.ToString());
        Int16 intFinal = Convert.ToInt16(d * 0.08);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 FayalCommented:
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
 
coagantusCommented:
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now