0
            if (Equation == "." || Equation == "")
                Answer = double.NaN;
            else
                Answer = (double)Equation.EvalNumerical();

            PreviousAnswerFlag = true;
            Equation = "";

            Answer = double.Parse(Answer.ToString("G7", CultureInfo.InvariantCulture));
            OutputBox.Content = Answer;

I am building a calculator using WPF and because there is a set size to the window, the box used to output the answer cuts it off at a specific point (Around 9 numbers excluding negative and decimal symbols). Currently I am using a NuGet package to parse the Equation in string form into an answer and am using .ToString() to convert the number into its exponent but this happens at around 14 digits and cuts off when the answer goes above 999,999,999.

I need something that will convert the number to exponential form after nine digits or something that will round the number to 9 digits that counts zeros (as Math.Round () does significant figures, the number is not always 9 digits long because it doesn't count initial zeros). I also need this to work regardless or positive, negative or decimal.

  • Does this answer your question? [Round a double to x significant figures](https://stackoverflow.com/questions/374316/round-a-double-to-x-significant-figures) – Poul Bak Sep 05 '22 at 22:11
  • Decimal number in a computer are stored a base two number. Converting from base two to base 10 give small errors like what you are seeing. The is no easy way to fix these rounding errors as a number and often the easiest solution is to convert to a string and then do rounding. – jdweng Sep 05 '22 at 22:13
  • @jdweng So something like converting the answer to a string and removing all digits after the 10th digit, then rounding the 9th digit using the 10th digit and shortening it to 9 digits. – IllusiVeXI _ 11 Sep 05 '22 at 22:21

1 Answers1

1
OutputBox.Content = string.Format("{0:E}", Answer);

Instead of doing all this complicated stuff, I have resorted to Just formatting the answer into Exponent form if the amount of digits exceeds nine.

  • One more little tip. Since you are making an application designed for «person» and not for «science», it is preferable to use `decimal` instead of `double`. – EldHasp Sep 06 '22 at 09:15