Beispiel #1
0
        public static Fraction CreateFraction(IEnumerable <OpenXmlElement> upper, IEnumerable <OpenXmlElement> lower)
        {
            var fraction = new Fraction();

            var numerator = new Numerator();

            numerator.Append(upper);
            var denominator = new Denominator();

            denominator.Append(lower);
            fraction.AppendChild(numerator);
            fraction.AppendChild(denominator);
            return(fraction);
        }
Beispiel #2
0
        /// <summary>
        /// Converts the unit to a string.
        /// </summary>
        /// <param name="IncludePrefix">If the prefix should be included in the string or not.</param>
        public string ToString(bool IncludePrefix)
        {
            StringBuilder Numerator      = null;
            StringBuilder Denominator    = null;
            int           NrDenominators = 0;

            foreach (KeyValuePair <AtomicUnit, int> Factor in this.factors)
            {
                if (Factor.Value > 0)
                {
                    if (Numerator is null)
                    {
                        if (IncludePrefix)
                        {
                            Numerator = new StringBuilder(Prefixes.ToString(this.prefix));
                        }
                        else
                        {
                            Numerator = new StringBuilder();
                        }
                    }
                    else
                    {
                        Numerator.Append('⋅');
                    }

                    Numerator.Append(Factor.Key.Name);

                    if (Factor.Value > 1)
                    {
                        if (Factor.Value == 2)
                        {
                            Numerator.Append('²');
                        }
                        else if (Factor.Value == 3)
                        {
                            Numerator.Append('³');
                        }
                        else
                        {
                            Numerator.Append('^');
                            Numerator.Append(Factor.Value.ToString());
                        }
                    }
                }
                else
                {
                    if (Denominator is null)
                    {
                        Denominator = new StringBuilder();
                    }
                    else
                    {
                        Denominator.Append('⋅');
                    }

                    NrDenominators++;
                    Denominator.Append(Factor.Key.Name);

                    if (Factor.Value < -1)
                    {
                        if (Factor.Value == -2)
                        {
                            Denominator.Append('²');
                        }
                        else if (Factor.Value == -3)
                        {
                            Denominator.Append('³');
                        }
                        else
                        {
                            Denominator.Append('^');
                            Denominator.Append((-Factor.Value).ToString());
                        }
                    }
                }
            }

            if (Numerator is null)
            {
                if (IncludePrefix)
                {
                    Numerator = new StringBuilder(Prefixes.ToString(this.prefix));
                }
                else
                {
                    Numerator = new StringBuilder();
                }
            }

            if (!(Denominator is null))
            {
                Numerator.Append('/');

                if (NrDenominators > 1)
                {
                    Numerator.Append('(');
                }

                Numerator.Append(Denominator.ToString());

                if (NrDenominators > 1)
                {
                    Numerator.Append(')');
                }
            }

            return(Numerator.ToString());
        }