-2

I'm creating a POS (Point Of Sale) and I've come across a problem when trying to convert the price "0.60" to an integer.

Data Background: All the Data for the Data Source is from a MySQL database that I have setup and connected with no problems.

The Price is Stored in a TextBox and is formatted as such "0.60", I believe that this is the reason why it isn't being converted. I keep getting the message below.

Additional information: Input string was not in a correct format.

        //Puts the Price Into a String.
        string NewPrice = txtPrice.Text;

        //Converts The Quantity In the TextBox field to a numbers.
        Quantity = Convert.ToInt32(txtQuant.Text);

        //Incorrect Format & Attempt One.
        //Price = Convert.ToInt32(NewPrice); <--- Problem.
        //Price = int.Parse(NewPrice);

        // I've also tried this method below with two '0' inside the { } brackets.
        // But Still No Luck.
        Price = Convert.ToInt32(string.Format("{0.00}",txtPrice.Text)); // <--- Problem.

        // Times Price & Quantity to get Total Price (0.60 * 2 = 1.20)
        TotalSingleItemPrice = Price * Quantity;

        // The Single Item Price is added into the overall total.
        TotalPrice += TotalSingleItemPrice;

        // Converts Total Item Price to String from Int.
        string TotalPriceStr = Convert.ToString(TotalSingleItemPrice);           

        // Puts TextBoxes / Strings Into One String array (I think).
        string[] InizialItemList = new string[] { cmboInitItem.Text, Convert.ToString(Price), Convert.ToString(Quantity), TotalPriceStr};

        // Adds The String Array Into the Data Grid View.
        DGVIIL.Rows.Add(InizialItemList);

I've tried to use the string.Format("{0.00}",txtPrice.Text) setup to combat this problem, I just can't see what I have over looked. I would like the Price to appear in my DataGridView - DGVIIL, as 0.60 if possible.

Logan Walker
  • 17
  • 1
  • 8

4 Answers4

3

0.60 is not an integer, the error is right on

Alternatives:

Decimal d = Decimal.Parse(txtPrice.Text);

Or even better:

Decimal d;
if ( decimal.TryParse(txtPrice.Text, out d) == false ){
  //handle bad number...
}
T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • This is a good answer but it could be better with a Decimal.TryParse(). – Rick Davin May 09 '14 at 19:14
  • 1
    Thank you, I've just used the Decimal d as you have stated and it worked. Now I will add TryParse() as Rick Davin stated and Yuval Itzchakov for Validation Reasons. – Logan Walker May 09 '14 at 19:16
1

You need to use decimal.Parse or Convert.ToDecimal as your string is clearly not an int. Decimal is recommanded as you're dealing with currency.

Price = Convert.ToDecimal(NewPrice);
Price = decimal.Parse(NewPrice);

Also, I suggest you look into TryParse to use for validation:

decimal price;
if (decimal.TryParse(NewPrice, out price))
{ // do stuff }
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
1

You'll need to convert it to double, then to int.

int x = Convert.ToInt32( Convert.ToDouble("1.11")); 

//Expected output: x = 1
MarzSocks
  • 4,229
  • 3
  • 22
  • 35
0
Price = Convert.ToInt32(string.Format("{0.00}",txtPrice.Text));

in the above code you are converting from a decimal style format to an integer. the int32 type can only contain whole integer numbers and thus the convert method will give you an error. Instead you can use the type of a double to hold your value properly.

double Price;
Price = Convert.ToDouble(string.Format("{0.00}",txtPrice.Text));
tezromania
  • 797
  • 1
  • 5
  • 18