Как сделать Match Type десятичным (в c #)? - PullRequest
0 голосов
/ 23 июня 2019

Я пытаюсь сделать квитанцию, но у меня возникли проблемы с настройкой макета по цене. Моя цена - десятичная дробь, а мой матч - строка, и они должны быть десятичными. Как мне это сделать?

    using System;
    using System.Text.RegularExpressions;

    public class Program
    {
        public static void Main(string[] args)
        {
             string itemName = " "; //variable for the name of the items
             int QTY = 0; //variable for the quantity of the items
             decimal subTotal = 0; //define subtotal of each item
            decimal SubTotalReceipt = 0; //define subtotal of total receipt
            decimal tax = 0;
            decimal total = 0;
            decimal price = 0;
            int counter = 0;
            bool flag = false;

            string[,] items = new string[15, 4];

            while (itemName != "0")
            {
                Console.WriteLine(); //output blank line
            Console.Write(" Item Name (press 0 to stop): "); //prompt user

            while (!flag)
            {
                itemName = Console.ReadLine(); //read user input; define itemName

                Match match = Regex.Match(itemName,
                    @"(A-za-z[0-9])" + "(A-za-z[0-9])",
                    RegexOptions.IgnoreCase);

                flag = match.Success;

                if (!flag)
                {
                    break;
                }
            }

            if (itemName == "0")
            {
                break;
            } //end if

            flag = false;

            Console.Write(" Item Price: $"); //prompt user

            while(!flag)
            {
                price = Convert.ToDecimal(Console.ReadLine()); //read user input; define price

                Match match = Regex.Match(price,
                    @"([0-9]\,)",
                    RegexOptions.IgnoreCase);

                flag = match.Success;

                if (!flag)
                {

                    break;
                }
            }

            Console.Write(" Quantity: "); //prompt user
            QTY = Convert.ToInt32(Console.ReadLine()); //read user input; define QTY (quantity)
            Console.WriteLine(); //output blank line

            //calculate subtotal for each item
            subTotal = price * QTY;

            //store the item details in the array
            items[counter, 0] = itemName;
            items[counter, 1] = price.ToString();
            items[counter, 2] = QTY.ToString();
            items[counter, 3] = subTotal.ToString();

            ++counter; //increment counter
        } //end while

        Console.WriteLine(" {0}{1,12}{2,16}{3,18}", " Item", "Price", "Quantity", "Subtotal"); //Organize receipt

        for (int i = 0; i < items.GetLength(0); i++) //begin for; used for 
    rows in receipt
        {
            for (int j = 0; j < items.GetLength(1); j++) //begin nested for; 
    used for columns in the receipt
            {
                Console.Write(items[i, j] + "\t\t");
            } //end nested for
            Console.WriteLine(); //output blank line
        } //end for

        //calculate subtotal of the total receipt
        SubTotalReceipt = SubTotalReceipt + subTotal;

        //calculate tax for the total receipt
        tax = SubTotalReceipt * 0.065M;

        //calculate total for the total receipt
        total = SubTotalReceipt + tax;

        Console.WriteLine(" Total items purchased: " + counter); //print number of items purchased
        Console.WriteLine(" Subtotal: {0:C}", SubTotalReceipt); //print subtotal of total receipt
        Console.WriteLine(" Tax: {0:C}", tax); //print tax
        Console.WriteLine(" Total: {0:C}", total); //print total price

        Console.ReadLine();
    } //end Main
} //end class

Я ожидал, что смогу преобразовать функцию Match, но это не сработало, и я не могу преобразовать десятичную цену. Я получаю код ошибки CS1503.

Вот пример моего желаемого вывода:

Имя элемента (нажмите 0, чтобы остановить): abc123 Цена товара: $ abc Цена товара (не может быть меньше 0): 3 $ Количество: 3

[Это повторяется до тех пор, пока Имя элемента = "0"]

...