Código:

``````int TopAttuale = Int32.Parse("1579.998779296875");
``````

dice

But that's what I have. How can I convert that string into INT (so 1579, I don't care about the comma)?

preguntado el 28 de agosto de 12 a las 11:08

You don't have a comma but a dot/decimal `.` If you don't care about it, remove it from the string. If you do care about what is after the decimal, than convert it to a float or double and round it off with Math.Ciel and convert it then to an int. -

## 7 Respuestas

Que Hacer:

``````(int) double.Parse(inputstring)
``````

Respondido 28 ago 12, 11:08

`int TopAttuale = (int)double.Parse("1579.998779296875");` the result on `Response.Write(TopAttuale);` is -2147483648? - Markzzz

@markzzz What are you talking about? `TopAttuale` mantendrá el valor `1579`. If you don't want to use parse methods from another reason, you can check out my answer. - Var simple

It sounds like you should parse it as a `decimal` first, then just cast to `int`. (Yo usaría `decimal` más bien que `double` as you've really tiene decimal input; `decimal` will represent your source data more accurately. It shouldn't make any difference though, by the time you cast it.)

``````// Parse using the invariant culture so that it always uses "." as the
// decimal separator
decimal parsed = decimal.Parse(text, CultureInfo.InvariantCulture);
int value = (int) parsed;
``````

Alternatively, see if there's a decimal point, and if so trim everything after it, then parse the result.

Respondido 28 ago 12, 11:08

Con `decimal` yo obtengo `Value was either too large or too small for an Int32.`. With double I get `-2147483648` as result :S - Markzzz

In fact double() returns `1,57999877929688E+15` : O - Markzzz

@markzzz: You shouldn't have done for the value you've given in the question. Perhaps you didn't give real sample data? Note the use of the invariant culture in my sample code. - jon skeet

``````decimal.ToInt32(decimal.Parse("1579.998779296875"))
``````

Respondido 28 ago 12, 11:08

you are trying to convert a floating point value in integer, which is directly not possible. but if u have to do it than first convert string in double then convert double in integer.

``````       string str = "1579.998779296875";
double val = Convert.ToDouble(str);
int val1 = Convert.ToInt32(val);
``````

Respondido 28 ago 12, 11:08

Trata `Decimal.Parse` método y emitir a `int`:

``````int TopAttuale = (int) Decimal.Parse(inputString)
``````

vea la sección `Decimal.TryParse` método:

``````string value;
decimal number;

value = "1579.998779296875";
if (Decimal.TryParse(value, out number))
int result = (int) number;
else
Console.WriteLine("Unable to parse '{0}'.", value);
``````

Respondido 28 ago 12, 11:08

This simply ignores everything that would come after the decimal point. Be warned, that this doesn't let you know if the string wasn't a number at all.

``````public int IntFromString(string str)
{
int result = 0;

foreach (char c in str)
{
if (!char.IsDigit(c))
{
break;
}

result = result * 10 + c - '0';
}

return result;
}
``````

Respondido 28 ago 12, 11:08

Prueba este código.

``````decimal a= decimal.Parse("1579.998779296875");
a=Math.Floor(a);
int b=(int)a;
Console.WriteLine(b);  ``````
It displays 1579. As you need.

Respondido 28 ago 12, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.