none
Calculating percentage of left over cost displaying result as NaN RRS feed

  • Question

  • double number;
                 double number1;
                 double result;
    
                 double.TryParse(SelectedQuoteForEditing.JobPrice, out number);  double.TryParse(lblRate.Content.ToString().ToString(), out number1);
    
                    var left = Math.Round((number - number1) / number * 100, 2);
    
                    result = left;
    
                   ERGPerc.Content = result.ToString("P") + "%";
    I am trying to show the percentage which is left after number1 has subtracted from number. The values I am calling contain a £ symbol and when running the code I get the result of "NaN" I guess this is to do with the £ sign as when removed code works. Anyone have any idea how I can fix this please ? I have been trying for hours with no luck :( Thanks in advance
    Friday, June 7, 2019 12:43 PM

Answers

  • So that is the currency symbol? Remove it before trying to call TryParse. The issue is that TryParse is failing (because it isn't a #). But you're not checking for that so `number` and/or `number` are invalid. Never call TryParse and not check the result. Also be aware that if SelectedQuoteForEditing is 0 you'll get a divide by 0 error. You're going to need to make this code far more error resilient.

    static void Main ( string[] args )
    {
        //I assume -1 is not a valid value for either of these so we'll use it as an indicator the price is wrong
        //var price = TryGetDecimal(SelectedQuoteForEditing.JobPrice);
        //var rate = TryGetDecimal(lblRate.Content);
        var price = TryGetDecimal("$45.7");
        var rate = TryGetDecimal("$8.6");
                
        if (price > 0 && rate > 0)
        {
            var percentage = (rate > 0) ? Math.Round((price - rate) / price * 100, 2) : 0;
            //ERGPerc.Content = percentage.ToString("P") + "%";
        } else
        {
            //This is an error state, you need to do something here...
        };
    }
    
    static decimal TryGetDecimal ( string value )
    {
        //Since you're dealing with currency, convert it according to the current UI culture
        //Add a using for System.Globalization
        if (Decimal.TryParse(value, NumberStyles.Currency, null, out var result))
            return result;
    
        return -1;
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 7, 2019 2:12 PM
    Moderator

All replies

  • So that is the currency symbol? Remove it before trying to call TryParse. The issue is that TryParse is failing (because it isn't a #). But you're not checking for that so `number` and/or `number` are invalid. Never call TryParse and not check the result. Also be aware that if SelectedQuoteForEditing is 0 you'll get a divide by 0 error. You're going to need to make this code far more error resilient.

    static void Main ( string[] args )
    {
        //I assume -1 is not a valid value for either of these so we'll use it as an indicator the price is wrong
        //var price = TryGetDecimal(SelectedQuoteForEditing.JobPrice);
        //var rate = TryGetDecimal(lblRate.Content);
        var price = TryGetDecimal("$45.7");
        var rate = TryGetDecimal("$8.6");
                
        if (price > 0 && rate > 0)
        {
            var percentage = (rate > 0) ? Math.Round((price - rate) / price * 100, 2) : 0;
            //ERGPerc.Content = percentage.ToString("P") + "%";
        } else
        {
            //This is an error state, you need to do something here...
        };
    }
    
    static decimal TryGetDecimal ( string value )
    {
        //Since you're dealing with currency, convert it according to the current UI culture
        //Add a using for System.Globalization
        if (Decimal.TryParse(value, NumberStyles.Currency, null, out var result))
            return result;
    
        return -1;
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 7, 2019 2:12 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,
    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 7:24 AM
    Moderator