none
Label/TextBox To Decimal

    Question

  • SuperMarket App Part 2 (CranberryAce Training)

    Ok guys I have another Issue this time I am trying to get a label to give the output of a decimal.

    decimal? unitPrice = Convert.ToDecimal(lalUnitPrice.Text);
           //    decimal? totPrice = Convert.ToDecimal(lalTotalPrice.Text);

    I Get The error message input String wasnt the correct format.

    I also Tried this  // unitPrice = decimal.Parse(this.lalUnitPrice.Text);
                        unitPrice = 5.59M;

    Samething ..........

    • Changed type CranberryAce Sunday, January 06, 2013 9:15 PM Because I t is
    Sunday, January 06, 2013 4:34 AM

Answers

  • When converting the text to a number you can only have numeric digits.  All letters must be removed.  You can use Regex to process the string

                string pattern = @"[\d\.\-,]+";
                Regex regex = new Regex(pattern);
                Match results = regex.Match("123.45m");
                string digits = results.Groups[0].ToString();


    jdweng

    ...or use decimal.TryParse:

    decimal outDecimal;
    decimal.TryParse(inString, out outDecimal);



    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |


    • Edited by VendorX Sunday, January 06, 2013 6:10 PM
    • Marked as answer by CranberryAce Monday, January 07, 2013 3:50 AM
    Sunday, January 06, 2013 6:08 PM
  • msdn remarks about TryParse:
    This overload differs from the Decimal.Parse(String) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed numeric value. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.

    ...and:

    if (listBox1.SelectedItem == (Object)"Heiniken")
    {
        lalItemName.Text = "Heiniken";
        pictureBox1.Image = SuperMarketConsoleWF.Properties.Resources.heineken_639_big;
        numValue = 1;
    
        //{ <- no need...
    
            try
            {
                // you using lalUnitPrice.Text to get unitPriceHeineken...
                unitPriceHeineken = decimal.Parse(this.lalUnitPrice.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid Value");
            }
            // ...and to display unitPriceHeineken...
            this.lalUnitPrice.Text = unitPriceHeineken.ToString();
    
        //} <- no need...
    }

    Try this:

    if (listBox1.SelectedItem == (Object)"Heiniken")
    {
        lalItemName.Text = "Heiniken";
        pictureBox1.Image = SuperMarketConsoleWF.Properties.Resources.heineken_639_big;
        numValue = 1;
        
        if(decimal.TryParse(inPrice, out unitPriceHeineken))
            lalUnitPrice.Text = unitPriceHeineken.ToString();
        else
            MessageBox.Show("Unable to parse " + inPrice);
    }
    ...inPrice is the price string.


    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |



    • Edited by VendorX Monday, January 07, 2013 2:31 AM
    • Marked as answer by CranberryAce Monday, January 07, 2013 3:49 AM
    Monday, January 07, 2013 2:24 AM
  • Because of:

    string inPrice = null;

    ..it must be a string of the product price.

    i.e.

    if (listBox1.SelectedItem == (Object)"Heiniken")
    {

    inPrice = ProductPrice.ToString().Trim('m');

    // ....
    }


    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |




    • Edited by VendorX Monday, January 07, 2013 3:32 AM
    • Marked as answer by CranberryAce Monday, January 07, 2013 3:49 AM
    Monday, January 07, 2013 3:23 AM

All replies

  • When converting the text to a number you can only have numeric digits.  All letters must be removed.  You can use Regex to process the string

                string pattern = @"[\d\.\-,]+";
                Regex regex = new Regex(pattern);
                Match results = regex.Match("123.45m");
                string digits = results.Groups[0].ToString();


    jdweng

    Sunday, January 06, 2013 11:05 AM
  • I don't understand how I should use this piece of code do I need to use a particular name space and how do I use the variable unitPrice to show in the label.text . I mean unitPrice = 5.0m to show in lbl.
    Sunday, January 06, 2013 5:00 PM
  • When converting the text to a number you can only have numeric digits.  All letters must be removed.  You can use Regex to process the string

                string pattern = @"[\d\.\-,]+";
                Regex regex = new Regex(pattern);
                Match results = regex.Match("123.45m");
                string digits = results.Groups[0].ToString();


    jdweng

    ...or use decimal.TryParse:

    decimal outDecimal;
    decimal.TryParse(inString, out outDecimal);



    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |


    • Edited by VendorX Sunday, January 06, 2013 6:10 PM
    • Marked as answer by CranberryAce Monday, January 07, 2013 3:50 AM
    Sunday, January 06, 2013 6:08 PM
  • The ‘m’ suffix is required in source code but is not needed in conversion functions. If your labels contains ‘m’ for some reasons, then before calling ToDecimal or TryParse remove the last ‘m’, for example:

    string t = lalUnitPrice.Text.Trim();

    if (t.EndsWith("M", StringComparison.InvariantCultureIgnoreCase)) t = t.Substring(0, t.Length - 1);

    decimal unitPrice = Convert.ToDecimal(t);



    • Edited by Viorel_MVP Sunday, January 06, 2013 6:28 PM
    Sunday, January 06, 2013 6:27 PM
  • For that is since framework 2 the decimal tryparse available

    http://msdn.microsoft.com/en-us/library/vstudio/9zbda557(v=vs.100).aspx

    Will you be so kind to change this thread in your original message from discussion to question.

    There is not any discussable thing in your message.


    Success
    Cor


    Sunday, January 06, 2013 6:34 PM
  • Ok guys this code works but i get the invalid value showing upp

    i also set unitPriceHeineken to a relevant price.

    I know if i remove the catch I will get an error, because i know the error is because I am trying to make the label give the decimal output. "Yes Heineken is spelt wrong"

     if (listBox1.SelectedItem == (Object)"Heiniken")
                {

                    lalItemName.Text = "Heiniken";
                    pictureBox1.Image = SuperMarketConsoleWF.Properties.Resources.heineken_639_big;
                    numValue = 1;
                    {
                        try
                        {

                            unitPriceHeineken = decimal.Parse(this.lalUnitPrice.Text);
                        }
                        catch (FormatException)
                        {
                            MessageBox.Show("Invalid Value");
                        }

                        this.lalUnitPrice.Text = unitPriceHeineken.ToString();
                    }


                }



    Monday, January 07, 2013 1:18 AM
  • msdn remarks about TryParse:
    This overload differs from the Decimal.Parse(String) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed numeric value. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.

    ...and:

    if (listBox1.SelectedItem == (Object)"Heiniken")
    {
        lalItemName.Text = "Heiniken";
        pictureBox1.Image = SuperMarketConsoleWF.Properties.Resources.heineken_639_big;
        numValue = 1;
    
        //{ <- no need...
    
            try
            {
                // you using lalUnitPrice.Text to get unitPriceHeineken...
                unitPriceHeineken = decimal.Parse(this.lalUnitPrice.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid Value");
            }
            // ...and to display unitPriceHeineken...
            this.lalUnitPrice.Text = unitPriceHeineken.ToString();
    
        //} <- no need...
    }

    Try this:

    if (listBox1.SelectedItem == (Object)"Heiniken")
    {
        lalItemName.Text = "Heiniken";
        pictureBox1.Image = SuperMarketConsoleWF.Properties.Resources.heineken_639_big;
        numValue = 1;
        
        if(decimal.TryParse(inPrice, out unitPriceHeineken))
            lalUnitPrice.Text = unitPriceHeineken.ToString();
        else
            MessageBox.Show("Unable to parse " + inPrice);
    }
    ...inPrice is the price string.


    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |



    • Edited by VendorX Monday, January 07, 2013 2:31 AM
    • Marked as answer by CranberryAce Monday, January 07, 2013 3:49 AM
    Monday, January 07, 2013 2:24 AM
  • Same thing unable to parse...

     decimal unitPricePasta = 5.59M, unitPriceShandy = 3.00M, unitPriceBounty = 59.34M,
                     unitPriceGatorade = 4.35M, unitPriceHeineken = 4.23M;
                string inPrice = null;
                if (listBox1.SelectedItem == (Object)"Heiniken")
                {
    
                    lalItemName.Text = "Heiniken";
                    pictureBox1.Image = SuperMarketConsoleWF.Properties.Resources.heineken_639_big;
                    numValue = 1;
    
                    {
                        if (decimal.TryParse(inPrice, out unitPriceHeineken))
                            lalUnitPrice.Text = unitPriceHeineken.ToString();
                        else
                            MessageBox.Show("Unable to parse " + inPrice);
                    }
                    {
                        if (listBox1.SelectedItem == (Object)"Heiniken")
                        {
                            lalItemName.Text = "Heiniken";
                            pictureBox1.Image = SuperMarketConsoleWF.Properties.Resources.heineken_639_big;
                            numValue = 1;




    Monday, January 07, 2013 2:54 AM
  • Because of:

    string inPrice = null;

    ..it must be a string of the product price.

    i.e.

    if (listBox1.SelectedItem == (Object)"Heiniken")
    {

    inPrice = ProductPrice.ToString().Trim('m');

    // ....
    }


    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |




    • Edited by VendorX Monday, January 07, 2013 3:32 AM
    • Marked as answer by CranberryAce Monday, January 07, 2013 3:49 AM
    Monday, January 07, 2013 3:23 AM
  • Thanks to everyone..

    He played the king as if afraid someone else would play the CranberryAce. ♣♣♣♣A♣♣♣♣♣♣♣♣A♣♣♣♣♣♣♣A♣♣♣♣♣♣♣A♣♣♣♣♣♣♣♣A♣♣♣♣♣♣♣♣♣A♣♣♣♣♣♣♣♣A♣♣

    Monday, January 07, 2013 3:51 AM