Answer the question
In order to leave comments, you need to log in
(windows.forms) Why doesn't it calculate correctly?
When I enter the number 6, it is squared, it turns out 36, it is divided by 10, it turns out 3.6 and is written to the variable k, in the second variable the number is rounded down, as a result, it turns out 3, then 3 is subtracted from 3.6, it should it will be 0.6, but in the output I see 0.59999.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double n, b, c, d = 0, k;
int a = 0;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char number = e.KeyChar;
if (!Char.IsDigit(number) && number != 8 && number != 44)
{
e.Handled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
label2.Text = "Введите число";
}
else
{
label2.Visible = false;
n = int.Parse(textBox1.Text);
for (int i = 1; i <= n; i++)
{
b = i;
c = i * i;
d = 0;
while (b >= 1)
{
b = b / 10;
d += 1;
}
for (int j = 1; j <= d; j++)
{
c = c / 10;
}
k = c;//здесь получается 3,6
c = Math.Floor(c);//здесь 3
k = k - c;//здесь 0,59999
for (int l = 1; l <= d; l++)
{
k = k * 10;
}
if (k == i)
{
a += 1;
}
}
label1.Text = a.ToString();
a = 0;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Welcome to float.
Because there are no integer calculations. 4/2 == 1.9999. Or 2.00001. Read about the representation of floating point numbers in memory
it turns out 3, then 3 is subtracted from 3.6, it should turn out 0.6
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question