コード例 #1
0
 public void ConvertToBinaryTest()
 {
     Assert.AreEqual("110100010001.110001", Convertions.ConvertDecimalToBinaryString(input: 3345.7680098M, precisionForBinary: 6, culture: new CultureInfo("ja-JP")));
     Assert.AreEqual("-10100100010100,11", Convertions.ConvertDecimalToBinaryString(input: -10516.7888M, precisionForBinary: 4, culture: new CultureInfo("fr-FR")));
     Assert.AreEqual("10000000000000000000000000000000", Convertions.ConvertDecimalToBinaryString(input: 2147483648M, precisionForBinary: 5, culture: new CultureInfo("uk-UA")));
     Assert.AreEqual("0.000110011", Convertions.ConvertDecimalToBinaryString(input: 0.1M, precisionForBinary: 10, culture: new CultureInfo("en-US")));
 }
コード例 #2
0
        private static void DisplayBinaryResult(decimal input, CultureInfo culture)
        {
            string binaryResult = Convertions.ConvertDecimalToBinaryString
                                      (input: input, precisionForBinary: PrecisionForBinaryResult, culture: culture);

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.Write("binary: ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(binaryResult);
        }
コード例 #3
0
        internal static string ExecuteExpression(string input, CultureInfo culture)
        {
            string decimalResult = null;

            try
            {
                var myNode = CalculationParser.GrowNodeTree(input: input, culture: culture);

                if (myNode != null)
                {
                    decimal result = DisplayDecimalResult(node: myNode, culture: culture);
                    decimalResult = result.ToString($"n{PrecisionForDecimalResult}", culture);
                    decimalResult = decimalResult.TrimEnd('0').TrimEnd(Separator(culture: culture));

                    string binaryResult = Convertions.ConvertDecimalToBinaryString
                                              (input: result, precisionForBinary: PrecisionForBinaryResult, culture: culture);

                    DisplayBinaryResult(input: result, culture: culture);

                    Console.ForegroundColor = ConsoleColor.White;

                    Notify?.Invoke();
                    Notify = null;
                }
            }
            catch (CheckingException exception)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Error: " + exception.Message);
            }
            catch (DivideByZeroException)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Error: Divide by Zero");
            }
            catch (OverflowException)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Error: Too large or too small value");
            }
            catch (Exception)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Error: Calculation failed");
            }

            return(decimalResult);
        }
コード例 #4
0
        internal CalcResult ExecuteExpression()
        {
            var decimalResult     = String.Empty;
            var binaryResult      = String.Empty;
            var hexadecimalResult = String.Empty;
            var decimalSeparator  = $"{Separator(culture: Culture)}";
            var message           = $"note, your decimal separator is a ' " + decimalSeparator + " '";

            if (String.IsNullOrWhiteSpace(Expression))
            {
                return(new CalcResult(decResult: decimalResult, binResult: binaryResult,
                                      hexResult: hexadecimalResult, message: message, separator: decimalSeparator));
            }

            try
            {
                var myNode = CalculationParser.GrowNodeTree(input: Expression, culture: Culture);

                if (myNode != null)
                {
                    decimal result = myNode.Calculate();

                    decimalResult = result.ToString($"n{PrecisionForDecimalResult}", Culture).
                                    TrimEnd('0').TrimEnd(Separator(culture: Culture));

                    binaryResult = Convertions.ConvertDecimalToBinaryString
                                       (input: Decimal.Parse(decimalResult, Culture), precisionForBinary: PrecisionForBinaryResult,
                                       culture: Culture);

                    hexadecimalResult = Convertions.ConvertDecimalToHexadecimalString
                                            (input: Decimal.Parse(decimalResult, Culture));

                    int signsAfterSeparator = SignsAfterSeparator(number: result, separator: decimalSeparator);

                    message = CheckOnFractionalRounding(afterRounding: decimalResult, precision: PrecisionForDecimalResult,
                                                        message: message, beforeRounding: result.ToString($"n{signsAfterSeparator}", Culture).
                                                        TrimEnd('0').TrimEnd(Separator(culture: Culture)));
                }
            }
            catch (CheckingException exception)
            {
                message = exception.Message;
            }
            catch (DivideByZeroException)
            {
                message           = "Divide by Zero gives you infinity";
                decimalResult     = Infinity;
                binaryResult      = Infinity;
                hexadecimalResult = Infinity;
            }
            catch (OverflowException)
            {
                message = "value is too large or too small";
            }
            catch (Exception)
            {
                message = "Calculation failed";
            }

            return(new CalcResult(decResult: decimalResult, binResult: binaryResult,
                                  hexResult: hexadecimalResult, message: message, separator: decimalSeparator));
        }