Search code examples
c#stringintvalueconverter

How to Convert string("1.0000") to int


The string format data with mostly 4 decimal places needs to be converted into int. I have tried this one but does not work and even directly using this Convert.ToInt16() but still did not worked:

Int32 result;
bool status = Int32.TryParse(v, out result);

Is there other ways to convert this?

Thanks.


Solution

  • You can convert it to Double first, and then convert to Int32

    String s = "1.0000";
    Double temp;
    
    Boolean isOk = Double.TryParse(s, out temp);
    
    Int32 value = isOk ? (Int32) temp : 0;