i have this code in my updatetobill code. im planning to round it off to nearest tenth i think? for example the price is 123456.123456, what i want to do is to have it as 123456.12 because since its a price, i need the cents. thanks in advance for any help :)
private void UpdateTotalBill()
{
double vat = 0;
double TotalPrice = 0;
long TotalProducts = 0;
foreach (DataListItem item in dlCartProducts.Items)
{
Label PriceLabel = item.FindControl("lblPrice") as Label; // get price
TextBox ProductQuantity = item.FindControl("txtProductQuantity") as TextBox; // get quantity
double ProductPrice = Convert.ToInt64(PriceLabel.Text) * Convert.ToInt64(ProductQuantity.Text); //computation fro product price. price * quantity
vat = (TotalPrice + ProductPrice) * 0.12; // computation for total price. total price + product price
TotalPrice = TotalPrice + ProductPrice+40 +vat;
TotalProducts = TotalProducts + Convert.ToInt32(ProductQuantity.Text);
}
Label1.Text = Convert.ToString(vat);
txtTotalPrice.Text = Convert.ToString(TotalPrice); // put both total price and product values and converting them to string
txtTotalProducts.Text = Convert.ToString(TotalProducts);
}
Just round it Math.Round like;
Math.Round(TotalPrice, 2) // 123456.12
Also you can use Math.Round(Double, Int32, MidpointRounding) overload to specify your MidpointRounding is AwayFromZero or ToEven. ToEven is the default option.
The best way to do this when you want to transform string is using the CurrencyFormat. You can use the code below:
txtTotalPrice.Text = TotalPrice.ToString("C");
txtTotalProducts.Text = TotalProducts.ToString("C");
Instead which:
txtTotalPrice.Text = Convert.ToString(TotalPrice);
txtTotalProducts.Text = Convert.ToString(TotalProducts);
Related
I've been getting this error whenever I run the application.
System.FormatException: 'Input string was not in a correct format.' on the decimal weight = Convert.ToDecimal(txtboxWeight.Text);
I don't know where I went wrong in this part this code is running from my different program.
private void ShowtotalPayment()
{
decimal weight = Convert.ToDecimal(txtboxWeight.Text);
decimal price = Convert.ToDecimal(lblPrice.Text);
int quantity = Convert.ToInt32(NumQuantity.Text);
decimal totalPayment = weight * price * quantity;
lblTotalPayment.Text = totalPayment.ToString();
};
For example, change:
decimal weight = Convert.ToDecimal(txtboxWeight.Text);
To:
decimal weight;
if (Decimal.TryParse(txtboxWeight.Text, out weight)) {
// ... do something with "weight" or proceed in here ...
}
else {
MessageBox.Show("Invalid Decimal in Weight!");
}
Let's say I have a string
string totalAmount = "100";
string price = "Price";
ALPHA = Price + totalAmount
I know that one line of the docket can have 42 characters.
I want to print this on a docket such that the "Price" is on the left hand side of the docket and the "totalAmount" is on the right hand side.
How would I do this?
PadRight should help you in this case.
string totalAmount = "100";
string price = "Price";
string result = price.PadRight(42 - totalAmount.Length) + totalAmount;
You can use the String.PadLeft / String.PadRight methods of the .net Framework if you use a font like Courier that have the same width for each character.
string totalAmount = "100".
ALPHA = "Price" + totalAmount.PadLeft(37, " ");
You can use String.PadRight and String.PadLeft to achive this.
string totalAmount = "100".PadLeft(37).
ALPHA = "Price" + totalAmount
I have this code as my computation for the bill.
private void UpdateTotalBill()
{
double vat = 0;
double TotalPrice = 0;
long TotalProducts = 0;
foreach (DataListItem item in dlCartProducts.Items)
{
Label PriceLabel = item.FindControl("lblPrice") as Label; // get price
TextBox ProductQuantity = item.FindControl("txtProductQuantity") as TextBox; // get quantity
double ProductPrice = Convert.ToInt64(PriceLabel.Text) * Convert.ToInt64(ProductQuantity.Text); //computation fro product price. price * quantity
vat = (TotalPrice + ProductPrice) * 0.12; // computation for vat
Math.Round(vat, 2);
TotalPrice = TotalPrice + ProductPrice;
TotalProducts = TotalProducts + Convert.ToInt32(ProductQuantity.Text);
}
Label1.Text =Convert.ToString(vat);
txtTotalPrice.Text = Convert.ToString(TotalPrice + 40 + vat); // add vat + 40 delivery charge to total price
txtTotalProducts.Text = Convert.ToString(TotalProducts);
}
Well what I want to do is to show the computation from the back end to front end with the use of labels. Like for example 1000(product price) x 3(product quantity) + vat(100) + delivery charge(4) = (total)4000. Something like this?
I think you want something like this,
lblResult.Text=lblPrice.Text+" (ProductPrice) * " +lblProductQuantity.Text + " + (Product Quantity) + "+vat+" (VAT) + " + lblDeliveryCharge.Text + " (Delivery Charge) = "+((Convert.ToDouble(lblPrice.Text)*Convert.ToDouble(lblProductQuantity.Text))+Convert.ToDouble(vat)+Convert.ToDouble(lblDeliveryCharge.Text));
To start with, you're performing calculations on monetary values - so you should be using decimal and not double. The former is for money and the latter for scientific values.
So, start with a delivery value defined:
var delivery = 40m;
Now compute the values of each item:
var items =
(
from item in dlCartProducts.Items.Cast<DataListItem>()
let PriceLabel = item.FindControl("lblPrice") as Label
let ProductQuantity = item.FindControl("txtProductQuantity") as TextBox
let price = decimal.Parse(PriceLabel)
let ProductQuantity = decimal.Parse(ProductQuantity)
let TotalPrice = price * quantity
let VAT = Math.Round(TotalPrice * 0.12)
select new
{
ProductQuantity,
TotalPrice,
VAT,
}
).ToArray();
Now you can easily compute the totals:
var totals = new
{
ProductQuantity = items.Sum(i => i.ProductQuantity),
TotalPrice = items.Sum(i => i.TotalPrice + i.VAT) + delivery,
VAT = items.Sum(i => i.VAT)
};
Now for the existing labels:
Label1.Text = totals.VAT.ToString();
txtTotalPrice.Text = totals.TotalPrice.ToString();
txtTotalProducts.Text = totals.ProductQuantity.ToString();
And finally, the label showing the calculation:
lblResult.Text = String.Format(
"{0}(product price) x {1}(product quantity) "
+ "+ vat({2}) + delivery charge({3}) = (total){4}",
(totals.TotalPrice - delivery - totals.VAT) / totals.ProductQuantity,
totals.ProductQuantity,
totals.VAT,
delivery,
totals.TotalPrice);
I'm trying to make a simple calculator using radio buttons in asp.net and c#. Two numbers are entered into a textbox each and a submit button is clicked and entered into a label.
The error I get is when the radio button is checked and submit is clicked, I get a runtime error; can anyone please help diagnose why?
This is my code:
int total;
if (rbAdd.Checked)
{
total = Convert.ToInt32(tbNo1.Text) + Convert.ToInt32(tbNo2.Text);
lblAns2.Text = total.ToString();
}
if (rbMult.Checked)
{
total = Convert.ToInt32(tbNo1.Text) * Convert.ToInt32(tbNo2.Text);
lblAns2.Text = total.ToString();
}
Simplified code that still fails the same way:
int total = 0;
int no1 = Convert.ToInt32(tbNo1.Text);
int no2 = Convert.ToInt32(tbNo2.Text);
if (rbAdd.Checked)
{
total = no1 + no2;
}
else if (rbMult.Checked)
{
total = no1 * no2;
}
lblAns2.Text = total.ToString();
I've tried your code and I got error message(Input string was not in a correct format.) only whenever I enter non integer values, because of your convert code Convert.ToInt32(tbNo1.Text);.
Do you want the entered values to be only integer? If so, please try this code instead (Modified from John Saunders code):
int total = 0;
int no1 = Convert.ToInt32(Math.Round(decimal.Parse(tbNo1.Text), 0));
int no2 = Convert.ToInt32(Math.Round(decimal.Parse(tbNo2.Text), 0));
if (rbAdd.Checked)
{
total = no1 + no2;
}
else if (rbMult.Checked)
{
total = no1 * no2;
}
lblAns2.Text = total.ToString();
You also have to validate the entered values as well to protect invalid characters.
Otherwise if you don't need to convert the entered values to be integer you could use this code:
decimal total = 0;
decimal no1 = decimal.Parse(tbNo1.Text);
decimal no2 = decimal.Parse(tbNo2.Text);
if (rbAdd.Checked)
{
total = no1 + no2;
}
else if (rbMult.Checked)
{
total = no1 * no2;
}
lblAns2.Text = total.ToString();
It's a little more complicated than that, but this is my code:
private void button1_Click(object sender, EventArgs e)
{
Decimal num1 = Convert.ToDecimal(textBox1.Text);
Decimal num2 = Convert.ToDecimal(textBox2.Text);
Decimal total = num1 + num2;
textBox3.Text = total.ToString("C");
Decimal total2 = Convert.ToDecimal(total);
total2 = total * 4.2;
textBox4.Text = Convert.ToString(total2);
Basically it's this: I have 4 text boxes, and I want to be able to put in a number in box1 and box2. Box three will multiply box 1 & 2 and convert it to currency. Box four will take Box 3s value and change it back to decimal and multiply a number. I can get it to work as long as total2 does not have a decimal. When it has one it will fail.
The program is basically a cash register program that you put in the following:
QTY (box 1)
Amonunt (box 2)
Subtotal (box 3)
Total (box 4)
Any ideas will be helpful.
Thanks,
Caleb
If I understand the problem correctly you perform operations on decimal variables holding currency values. Often it means maintaining a specific resolution (e.g 2 digits after decimal point for cents). Since Decimal is a general purpose type you need to maintain the required resolution programmatically.
example:
static Decimal RoundToCents(Decimal value)
{
return Math.Round(value, 2, MidpointRounding.AwayFromZero);
}
Decimal num1 = RoundToCents(Convert.ToDecimal(textBox1.Text));
Decimal num2 = RoundToCents(Convert.ToDecimal(textBox2.Text));
Decimal total = num1 + num2; // no rounding is needed for additions and subtractions
Decimal total2 = RoundToCents(total * 4.2m);
The fundamental problem is that you are mixing decimal and double in this line:
total2 = total * 4.2;
total is decimal and 4.2 is a double literal. To write a decimal literal use the m suffix.
total2 = total * 4.2m;