System.FormatException: 'Indatasträngen hade ett felaktigt format.'

Permalänk

System.FormatException: 'Indatasträngen hade ett felaktigt format.'

tja,
håller att lära mig C# i koden får jag detta fel "System.FormatException: 'Indatasträngen hade ett felaktigt format." som jag förstår och inte vet hur jag ska lösa det

public partial class frmInvoiceTotal : Form
{
public frmInvoiceTotal()
{
InitializeComponent();
}

int numberOfInvoices = 0;
decimal totalOfInvoices = 0m;
decimal invoiceAverage = 0m;
private void BtnCalculate_Click(object sender, EventArgs e)
{
decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
decimal discountPercent = .25m;
decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
decimal invoiceTotal = subtotal - discountAmount;

txtSubtotal.Text = subtotal.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");

numberOfInvoices++;
totalOfInvoices += invoiceTotal;
invoiceAverage = totalOfInvoices / numberOfInvoices;

txtNumberOfInvoices.Text = numberOfInvoices.ToString();
txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
txtInvoiceAverage.Text = invoiceAverage.ToString("c");

txtEnterSubtotal.Text = "";
txtEnterSubtotal.Focus();

}

Permalänk
Medlem
Skrivet av TaskMaster:

tja,
håller att lära mig C# i koden får jag detta fel "System.FormatException: 'Indatasträngen hade ett felaktigt format." som jag förstår och inte vet hur jag ska lösa det

public partial class frmInvoiceTotal : Form
{
public frmInvoiceTotal()
{
InitializeComponent();
}

int numberOfInvoices = 0;
decimal totalOfInvoices = 0m;
decimal invoiceAverage = 0m;
private void BtnCalculate_Click(object sender, EventArgs e)
{
decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
decimal discountPercent = .25m;
decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
decimal invoiceTotal = subtotal - discountAmount;

txtSubtotal.Text = subtotal.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");

numberOfInvoices++;
totalOfInvoices += invoiceTotal;
invoiceAverage = totalOfInvoices / numberOfInvoices;

txtNumberOfInvoices.Text = numberOfInvoices.ToString();
txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
txtInvoiceAverage.Text = invoiceAverage.ToString("c");

txtEnterSubtotal.Text = "";
txtEnterSubtotal.Focus();

}

Om du testar att mata in decimal tal med ',' (kommatecken) istället för '.' (punkt) eller tvärt om, blir det skillnad då?

Som man kan se i dokumentationen så finns en version av funktionen Convert.ToDecimal() som tar två argument, varav en är en IFormatProvider, som bestämmer förväntat format för strängen som användaren matar in. Ifall du vill att man ska kunna mata in både kommatecken och punkt så behöver du specificera en provider som t.ex:

CultureInfo.InvariantCulture

Tips, använd [code][/code] taggar runt din kod för att behålla formatteringen på Sweclockers

Visa signatur

| MSI B650 Tomahawk | Ryzen 7 9800X3D | ASUS RTX 3070 | 64GB DDR5 6000MHz | MSI MPG A1000G | Samsung 970 Evo M.2 1TB + 2x WD Black SN850X 2TB|

Permalänk

Tackar!!