C# .NET - remove decimal point in string - Asked By Sarah Nel on 10-May-10 03:20 AM

I've got a string that returns a price with commas between thousands and decimal point. But then I need an int value of the same cost, so I've used the .Replace and .Remove properties, the .Replace works but .Remove gives this error:

CS1502: The best overloaded method match for 'string.Remove(int)' has some invalid arguments

<code>
string cost = row["Construction Cash Flow"].ToString();
                string cost2 = cost.Replace(",", "");
                       cost2 = cost.Remove(".");
                int row1 = (cost2 != null) ? Convert.ToInt32(cost2) : 0;
</code>
DL M replied to Sarah Nel on 10-May-10 03:24 AM
here sample code will help try this link

http://idunno.org/archive/2004/14/01/122.aspx
Sreekumar P replied to Sarah Nel on 10-May-10 03:26 AM
string cost = row["Construction Cash Flow"].ToString();
                string cost2 = cost.Replace(",", "");
                       cost2 = cost.Replace(".","");
                int row1 = (cost2 != null) ? Convert.ToInt32(cost2) : 0;


for removing also u can use Replace
Sagar P replied to Sarah Nel on 10-May-10 03:27 AM
I think no need to use any string operation. You can do it like this;

string price = "1,234.50";

int actualPrice =Convert.ToInt32(Convert.ToDouble(price)); //It will give you 1234 as your actual price in int...

Mash B replied to Sarah Nel on 10-May-10 03:29 AM
U can use same Replace function for ur need

cost.Replace(".","");

Remove method dosent accept string parameter,

defination of remove method
Remove(int startIndex)     // startindex is the position to begin deleting character
Remove(int startIndex,int Count) // count is number of character to delete from start index
Sarah Nel replied to Sagar P on 10-May-10 03:32 AM
Thanks I used, and it works perfect:
int actualPrice =Convert.ToInt32(Convert.ToDouble(price));



Sagar P replied to Sarah Nel on 10-May-10 03:36 AM
:-) cheers!!!!!!!!!!!

But before doing it make sure your string value is empty or not... beacuase if it's empty it will give you error.. so add something like before this;

if(!string.IsNullOrEmpty(price))
{
    int actualPrice =Convert.ToInt32(Convert.ToDouble(price));
}
Anoop S replied to Sarah Nel on 10-May-10 03:37 AM
You can also remove dot like this way

    string yourString = "abc.cd";
    yourString = yourString.Replace(".", "");