Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

if statement - Problem with parsing Variable to Return C# -Script Task Transformation

Hey there guys have a little Problem with my Return Value, it will be mapped to the empty string from the beginning. As I am new to c# it might be easy to answer.

Query:

Private static Decimal parseVatWithoutBla(string originalString) // 4,5 inserted {

    var cleanedString = string.Empty;
    //var cleanedString = originalString;

        if (originalString.IndexOf(",") > 0)
        {
            if (originalString.IndexOf(".") > originalString.IndexOf(","))
            {
                cleanedString = originalString.Replace(",", "").Replace(".", ",");
             }
        }
        else if (originalString.IndexOf(".") > 0)
        {
            if (originalString.IndexOf(",") > originalString.IndexOf("."))
            {
                cleanedString = originalString.Replace(".", "");
            }
        }
        else if (originalString.IndexOf(",") == -1 && originalString.IndexOf(".") > 0)
        {
            cleanedString = originalString.Replace(".", ",");
        }
        else
        {
        cleanedString = originalString; //cleanedString =4,5
        }
        return Convert.ToDecimal(cleanedString); // returns the Empty string not the 4,5 why?!

added the comments to describe the problem.

Ty

question from:https://stackoverflow.com/questions/65919303/problem-with-parsing-variable-to-return-c-sharp-script-task-transformation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
if (originalString.IndexOf(",") > 0 && originalString.IndexOf(".") > originalString.IndexOf(","))
    {
            cleanedString = originalString.Replace(",", "").Replace(".", ",");
         
    }
    else if (originalString.IndexOf(".") > 0 && originalString.IndexOf(",") > originalString.IndexOf("."))
    {
            cleanedString = originalString.Replace(".", "");
        
    }
    else if (originalString.IndexOf(",") == -1 && originalString.IndexOf(".") > 0)
    {
        cleanedString = originalString.Replace(".", ",");
    }
    else
    {
    cleanedString = originalString; //cleanedString =4,5
    }
    return Convert.ToDecimal(cleanedString);

Becuse your "else" never run. Try this if-else combination


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...