0

It should be a simple code:

        string qty = "211.0000";
        Console.WriteLine(Int32.Parse(qty));

I tried to use Convert.ToInt32 or Int32.Parse, but they all throw an error: Error(s): Exception in user code:

System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at Rextester.Program.Main(String[] args)

Did I make any errors to define a string and give a value? I have tried use string or String to define the variable. It works if I use Console.WriteLine(Convert.ToInt32("211.0000")).

BTW I was testing in http://rextester.com/

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
James Chen
  • 833
  • 3
  • 10
  • 23
  • 5
    `"211.0000"` is not a valid *integer* value but a *floating point* one. – Dmitry Bychenko Apr 04 '17 at 19:15
  • Because it's not an integer – Rufus L Apr 04 '17 at 19:17
  • @Dmitry Bychenko But I was trying to define it as a string first by using double quotes, and then convert it to int. At least Convert.ToInt32 should works isn't it? – James Chen Apr 04 '17 at 19:17
  • 2
    Integers don't have '.' characters in them – Rufus L Apr 04 '17 at 19:17
  • @Rufus L I am sorry I am confused, shouldn't both of Convert.ToInt32 or Int32.Parse are convert string to number? – James Chen Apr 04 '17 at 19:18
  • @JamesChen They do, but only if the string follows the correct *format*. Integers cannot contain decimal points, therefore you cannot parse it as an Int32. – Quietust Apr 04 '17 at 19:19
  • 1
    @James Only if the string can be converted will they work. Just as you cannot convert "123A000" to an integer, you cannot convert "123.000" to an integer (using those functions) – Rufus L Apr 04 '17 at 19:20

4 Answers4

5

When working with floating point values (which have explicit decimal separator .) you should use decimal or double:

  using System.Globalization;

  ...

  string qty = "211.0000";

  // be careful: different cultures have different decimal separators
  decimal result = decimal.Parse(qty, CultureInfo.InvariantCulture);

or

  double result = double.Parse(qty, CultureInfo.InvariantCulture);

having got floating point representation you can round it up to integer

  int value = (int) (result > 0 ? result + 0.5 : result - 0.5); 

or

  int value = (int) (Math.Round(result)); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
2

I'm changing my answer, hope that's ok, but here's probably the best thing for you is to do a decimal.TryParse, which can handle leading characters like - and ., and then just cast the result to an integer. If the parsing fails, it returns a zero (in the case where the string is not a decimal):

decimal result;
Console.WriteLine(decimal.TryParse(qty, NumberStyles.Any, 
    CultureInfo.InvariantCulture, out result) ? (int)result : 0);

The same code could be used to store the int for later use:

decimal result;
int intResult = decimal.TryParse(qty, NumberStyles.Any,
    CultureInfo.InvariantCulture, out result) ? (int) result : 0;
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

You need to use Decimal instead of int

Decimal.Parse(qty);

If you just need the integer part then use the following snippet

Double.Parse(qty, CultureInfo.InvariantCulture);
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
0

The value you provided is not an integer, before everything what you might try is converting it to a decimal and after that use Convert.ToInt32 from mscorlib because Integers cannot contain decimal points, soo that means you cannot parse it as an Int32, please follow next example:

string value = String.Format("{0:0.00}", 123.4567);      // "123.46"
decimal d = decimal.Parse(value);
Convert.ToInt32(d);
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • it gives me 2110000, but I want 211 – James Chen Apr 04 '17 at 19:20
  • @JamesChen check for edit – Roxy'Pro Apr 04 '17 at 19:22
  • @JamesChen If you do not specify culture info (`IFormatProvider`), the current one will be used. On `rextester.com` that will be _German (Germany)_. Therefore a comma is required if you want a decimal separator. So `decimal.Parse("211,0000")` will give `211` with that culture, while `decimal.Parse("211.0000")` gives `2110000`. – Jeppe Stig Nielsen Apr 04 '17 at 20:12