0

I am working on a calculator in Visual Studio 2019 and I have had a problem. I need to multiply a variable value (let's call it X) by a constant one (let's call it Y = 400). The value X is obtained after doing another mathematical operation, but it shows an exaggerated number of decimal places that when multiplying by Y is incorrect.

Suppose I get X = 1.47857142857143. I only need the first 2 decimals (.47) and whatever integer that results.

Incorrect result: 1.47857142857143 * 400 = 591.4285

Correct result: 1.47 * 400 = 588.

As final data: 1.- I have to use the result for other operations; so hiding it with the margins of the label or the textbox does not work for me. 2.-I only have knowledge of the C++ language and using C#, but many years have passed since I used them, so I would appreciate it if the answers were as detailed as possible.

LuisMi
  • 1
  • Does this answer your question? [Truncate Two decimal places without rounding](https://stackoverflow.com/questions/3143657/truncate-two-decimal-places-without-rounding) – Henry Twist Sep 05 '21 at 00:39
  • 1
    You're in control. Consider writing down all the rules of your calculator (for example: always _truncate_ `X` to two decimal places). Then figure out how to implement the rules (like following the link from @Henry). Get the rules right and the software should follow. – Flydog57 Sep 05 '21 at 01:51

1 Answers1

0

That is wrong first question for your problem. First you should decide that what is the max digits for your X, and result. I think Y is always integer. If X have max 4 digits and result have max 2 digits , your result always goes to wrong mathematically. First pls check Math.Round

serdar
  • 1
  • 3