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.
string value = "34690.42724"; Convert. ToInt64(Convert. ToDouble(value)); c#
ToDecimal(String, IFormatProvider) Converts the specified string representation of a number to an equivalent decimal number, using the specified culture-specific formatting information.
Use the str. split() method to split the string into a list of strings. Use the map() function to convert each string into an integer.
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;
                        You can use the following:
string data = "1.0000";
int number
if(data.Contains('.'))
    number = int.Parse(data.Substring(0, data.IndexOf('.'))); //Contains decimal separator
else
    number = int.Parse(data); //Contains only numbers, no decimal separator.
Because 1.0000 has decimal places, first strip those from the string, and then parse the string to int.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With