This program bring to random numbers in a decimal way to get sum using ran.next and are stored in the variables sum1a (for the first number) and sum2a (for the second number) and printed in "Suma1.Text"; the operator need to captured the result in the textbox called "Resultado" how has the name "Resultado1" and stored in the variable result1, then is compared with the sum of sum1a and sum2a and stored in total1 to get compared with result1, if total1 match with result1 have to print "Correcto" or "Incorrecto" when there is no match, but sometimes even when the result is correct said that is "Incorrecto".
[Calculator vs Program][1]
namespace Sumas_punto_decimal_prueba
{
public partial class Form1 : Form
{
double sum1a, sum2a;
string num1a, num2a;
double total1;
double result1;
public Form1()
{
InitializeComponent();
}
private void Generar_Click(object sender, EventArgs e)
{
//LIMPIADOR
foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
TextBox text = ctrl as TextBox;
text.Clear();
}
}
Random ran = new Random();
//SUMA 1
sum1a = ran.Next(100 , 10000) / 100.00;
sum2a = ran.Next(100 , 10000) / 100.00;
num1a = sum1a.ToString("##.#0");
num2a = sum2a.ToString("##.#0");
if (sum1a < 10 && sum2a < 10)
{
Suma1.Text = " 0" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 1 && sum2a < 1)
{
Suma1.Text = " 00" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 10 && sum2a < 1)
{
Suma1.Text = " 0" + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 1 && sum2a < 10)
{
Suma1.Text = " 00" + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 10)
{
Suma1.Text = " 0" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum1a < 1)
{
Suma1.Text = " 00" + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum2a < 10)
{
Suma1.Text = " " + num1a + "\r\n +0" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum2a < 1)
{
Suma1.Text = " " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else if (sum2a < 1)
{
Suma1.Text = " " + num1a + "\r\n +00" + num2a + "\r\n¯¯¯¯¯¯¯";
}
else
{
Suma1.Text = " " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
}
}
private void Calificar_Click(object sender, EventArgs e)
{
//SUMA NUMERO 1
double.TryParse(Resultado1.Text, out result1);
total1 = sum1a + sum2a;
if (result1 == total1)
{
Validacion1.Text = "Correcto";
}
else
{
Validacion1.Text = "Incorrecto. \r\nEl resultado es: " + total1;
}
}
}
}
What can be the reason that sometimes that error happen? I make a stimated and happen 1 of 15 times.
Thank to all for the help but already found a solution, using Math.Round() and "Zero placeholder"
sum1a = ran.Next(10000) / 100.00;
sum2a = ran.Next(10000) / 100.00;
sum1a = Math.Round(sum1a, 2, MidpointRounding.AwayFromZero);
sum2a = Math.Round(sum2a, 2, MidpointRounding.AwayFromZero);
num1a = sum1a.ToString("00.00");
num2a = sum2a.ToString("00.00");
Suma1.Text = " " + num1a + "\r\n +" + num2a + "\r\n¯¯¯¯¯¯¯";
Thanks. uwu