C
C#2y ago
johndoe

❔ Wrong calculation output

So this is my final code:
void AngebotHinzufuegen(object sender, RoutedEventArgs e)
{
decimal listeneinkaufspreis = Convert.ToDecimal(menge.Text) * Convert.ToDecimal(listenpreis.Text) + Convert.ToDecimal(mindermengenzuschlag.Text);
decimal lieferrabatt = listeneinkaufspreis * Convert.ToDecimal(rabatt.Text) / 100;
decimal lieferskonto = listeneinkaufspreis * Convert.ToDecimal(skonto.Text) / 100;

VergleichDataGrid.Items.Add(new DataItem
{
Anbieter = anbieter.Text,
Menge = menge.Text,
Listenpreis = listenpreis.Text,
Mindermengenzuschlag = mindermengenzuschlag.Text,
Rabatt = rabatt.Text,
Skonto = skonto.Text,
Transportkosten = transportkosten.Text,
Listeneinkaufspreis = listeneinkaufspreis,
Lieferrabatt = lieferrabatt,
Lieferskonto = lieferskonto,
TransportkostenBerechnet = transportkosten.Text,
Bezugspreis = listeneinkaufspreis - lieferrabatt - lieferskonto + Convert.ToDecimal(transportkosten.Text)
});

}
void AngebotHinzufuegen(object sender, RoutedEventArgs e)
{
decimal listeneinkaufspreis = Convert.ToDecimal(menge.Text) * Convert.ToDecimal(listenpreis.Text) + Convert.ToDecimal(mindermengenzuschlag.Text);
decimal lieferrabatt = listeneinkaufspreis * Convert.ToDecimal(rabatt.Text) / 100;
decimal lieferskonto = listeneinkaufspreis * Convert.ToDecimal(skonto.Text) / 100;

VergleichDataGrid.Items.Add(new DataItem
{
Anbieter = anbieter.Text,
Menge = menge.Text,
Listenpreis = listenpreis.Text,
Mindermengenzuschlag = mindermengenzuschlag.Text,
Rabatt = rabatt.Text,
Skonto = skonto.Text,
Transportkosten = transportkosten.Text,
Listeneinkaufspreis = listeneinkaufspreis,
Lieferrabatt = lieferrabatt,
Lieferskonto = lieferskonto,
TransportkostenBerechnet = transportkosten.Text,
Bezugspreis = listeneinkaufspreis - lieferrabatt - lieferskonto + Convert.ToDecimal(transportkosten.Text)
});

}
The formulas are correct, but there has to be some weird conversion mistake I made because the result which comes for Bezugspreis is completely off. Anyone an idea where i made a mistake? Here an example entry and which result should come out: menge.Text = 50 listenpreis.Text = 4 mindermengenzuschlag.Text = 2.5 rabatt.Text = 0 skonto.Text = 0 transportkosten.Text = 4.95 Calculated values: listeneinkaufspreis = 202.5 lieferrabatt = 0 lieferskonto = 0 transportkostenBerechnet = 4.95 And the correct final result: bezugspreis = 207.45 I get the wrong result (750)
15 Replies
Pobiega
Pobiega2y ago
use the debugger? $debug
MODiX
MODiX2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Pobiega
Pobiega2y ago
also, its conventional to name all your variables and stuff in english, even if the app uses... german? dutch? localization in the frontend
Google
Google2y ago
to make it maybe a little easier to debug, create the object separately, then add it to the list. Then you might be able to observe one instance of the DataItem easier than scrolling through the collection (VergleichDataGrid.Items) more easily.
johndoe
johndoeOP2y ago
Yeah German. I want to make it English but our teachers force us to use German so they understand the variables etc better
Pobiega
Pobiega2y ago
kek but yeah, as james suggested, do things in a few more inbetween steps so you can inspect the values with the debugger that lets you check that each variable has the value you expect as you go
johndoe
johndoeOP2y ago
But is there no obvious conversion error in my code which states directly that the result cannot be right Because it is so far off
Pobiega
Pobiega2y ago
some value somewhere isnt getting the value you expect. thats it. I'm not gonna do the math on this one when the answer so very clearly is: "use the debugger" 😛
johndoe
johndoeOP2y ago
Ok I will check it. Because I know the formulas are correct (tested before with JavaScript)
Pobiega
Pobiega2y ago
could be the classic . vs , with decimal
johndoe
johndoeOP2y ago
I only used . So far. I will test , in a Bit Okay @Pobiega, you were right. With commas as TextBox input every calculation is correct. How could I also make dots work the same way?
Pobiega
Pobiega2y ago
this is because what number is the decimal separator is "culture specific" you can specify what culture to parse a number as, but there is no good way to support both at the same time, because what does 30,40.50 parse as?
johndoe
johndoeOP2y ago
I mean could I not just use the replace method to replace all dots with commas before it is being parsed
Pobiega
Pobiega2y ago
is 30,40,50 easier to parse? 🙂 I'd suggest removing the ability to write everything except numbers and . and then use a culture you know handles dots
Add a preview text input event. Like so: <TextBox PreviewTextInput="PreviewTextInput" />. Then inside that set the e.Handled property if the text isn't allowed. e.Handled = !IsTextAllowed(e.Text);
alternatively, and probably better, is to make a Behavior for this
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server