3

i have 2 or maybe more than 2 string variable like below

string a = csvalue[7].Replace(" ", ""); //a="‏120641"
string b = csvalue[9].Replace(" ", "") ;//b="‏"221707‏‏‏‏
decimal result = Convert.ToDecimal(a) + Convert.ToDecimal(b)

but it have exception:" threw an exception of type 'System.FormatException'"

i try

       enter code here 
       string one = "‏120641‏‏‏‏";
        string two = "‏221707‏‏‏‏";
        string three = "123548";
       
        Double iOne = 0;
        Double iTwo = 0;
        Double ithree = 0;

        Double.TryParse(one, out iOne);
        Double.TryParse(two, out iTwo);
        Double.TryParse(three, out ithree);

and it dosent work for iOne,iTwo please help me

5
  • your input has some RLM-unicode-markers that break the formatting. you have to replace those as well, not just spaces. Commented Jul 12, 2021 at 10:47
  • dotnetfiddle.net/fenzWO it runs for me, what seems to be the problem? Commented Jul 12, 2021 at 10:49
  • @Costa: copy&paste the code from here to your fiddle and you'll see the problem Commented Jul 12, 2021 at 11:05
  • try printing one.Length - it's 11 instead of the expected 6 Commented Jul 12, 2021 at 12:05
  • 1
    And this is why actual code in the question is much better than an image of it! Commented Jul 12, 2021 at 12:08

1 Answer 1

1

There are some (invisible) unicode chars which are not digits, so cannot be parsed. You could remove them for example with Linq and char.IsDigit:

using System;
using System.Linq;
                    
public class Program
{
    public static void Main()
    {
        string one = "‏120641‏‏‏‏";
        string two = "‏221707‏‏‏‏";
        string three = "123548";
        one = new string(one.Where(char.IsDigit).ToArray());
        two = new string(two.Where(char.IsDigit).ToArray());
        three = new string(three.Where(char.IsDigit).ToArray());
       
        Double iOne = 0;
        Double iTwo = 0;
        Double ithree = 0;

        Double.TryParse(one, out iOne);
        Double.TryParse(two, out iTwo);
        Double.TryParse(three, out ithree);
        Console.WriteLine(iOne);
        Console.WriteLine(iTwo);
        Console.WriteLine(ithree);
    }
}

.Net Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.