Ejemplo n.º 1
0
        private static string DecimalNumberFormat(UnitStyleFormat format)
        {
            char[] zeroes = new char[format.LinearDecimalPlaces + 2];
            if (format.SupressLinearLeadingZeros)
            {
                zeroes[0] = '#';
            }
            else
            {
                zeroes[0] = '0';
            }

            zeroes[1] = '.';

            for (int i = 2; i < zeroes.Length; i++)
            {
                if (format.SupressLinearTrailingZeros)
                {
                    zeroes[i] = '#';
                }
                else
                {
                    zeroes[i] = '0';
                }
            }
            return(new string(zeroes));
        }
Ejemplo n.º 2
0
        public static string ToFractional(double length, UnitStyleFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            int num = (int)length;
            int numerator;
            int denominator;

            GetFraction(length, (short)Math.Pow(2, format.LinearDecimalPlaces), out numerator, out denominator);
            if (numerator == 0)
            {
                return(string.Format("{0}", (int)length));
            }

            string text = string.Empty;

            switch (format.FractionType)
            {
            case FractionFormatType.Diagonal:
                text = "\\A1;" + num + "{\\H" + format.FractionHeightScale + "x;\\S" + numerator + "#" + denominator + ";}";
                break;

            case FractionFormatType.Horizontal:
                text = "\\A1;" + num + "{\\H" + format.FractionHeightScale + "x;\\S" + numerator + "/" + denominator + ";}";
                break;

            case FractionFormatType.NotStacked:
                text = num + " " + numerator + "/" + denominator;
                break;
            }
            return(text);
        }
Ejemplo n.º 3
0
        public static string ToDegreesMinutesSeconds(double angle, UnitStyleFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            double degrees = angle;
            double minutes = (degrees - (int)degrees) * 60;
            double seconds = (minutes - (int)minutes) * 60;

            NumberFormatInfo numberFormat = new NumberFormatInfo
            {
                NumberDecimalSeparator = format.DecimalSeparator
            };

            if (format.AngularDecimalPlaces == 0)
            {
                return(string.Format(numberFormat, "{0}" + format.DegreesSymbol, (int)Math.Round(degrees, 0)));
            }
            if (format.AngularDecimalPlaces == 1 || format.AngularDecimalPlaces == 2)
            {
                return(string.Format(numberFormat, "{0}" + format.DegreesSymbol + "{1}" + format.MinutesSymbol, (int)degrees, (int)Math.Round(minutes, 0)));
            }
            if (format.AngularDecimalPlaces == 3 || format.AngularDecimalPlaces == 4)
            {
                return(string.Format(numberFormat, "{0}" + format.DegreesSymbol + "{1}" + format.MinutesSymbol + "{2}" + format.SecondsSymbol, (int)degrees, (int)minutes, (int)Math.Round(seconds, 0)));
            }
            // the suppression of leading or trailing zeros is not applicable to DegreesMinutesSeconds angles format
            string f = "0." + new string('0', format.AngularDecimalPlaces - 4);

            return(string.Format(numberFormat, "{0}" + format.DegreesSymbol + "{1}" + format.MinutesSymbol + "{2}" + format.SecondsSymbol, (int)degrees, (int)minutes, seconds.ToString(f, numberFormat)));
        }
Ejemplo n.º 4
0
        public static string ToDecimal(double length, UnitStyleFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            NumberFormatInfo numberFormat = new NumberFormatInfo
            {
                NumberDecimalSeparator = format.DecimalSeparator
            };

            return(length.ToString(DecimalNumberFormat(format), numberFormat));
        }
Ejemplo n.º 5
0
        public static string ToRadians(double angle, UnitStyleFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            NumberFormatInfo numberFormat = new NumberFormatInfo
            {
                NumberDecimalSeparator = format.DecimalSeparator
            };

            return((angle * MathHelper.DegToRad).ToString(DecimalNumberFormat(format), numberFormat) + format.RadiansSymbol);
        }
Ejemplo n.º 6
0
        public static string ToEngineering(double length, UnitStyleFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            NumberFormatInfo numberFormat = new NumberFormatInfo
            {
                NumberDecimalSeparator = format.DecimalSeparator
            };
            int    feet   = (int)(length / 12);
            double inches = length - 12 * feet;

            if (MathHelper.IsZero(inches))
            {
                if (feet == 0)
                {
                    if (format.SupressZeroFeet)
                    {
                        return(string.Format("0{0}", format.InchesSymbol));
                    }
                    if (format.SupressZeroInches)
                    {
                        return(string.Format("0{0}", format.FeetSymbol));
                    }
                    return(string.Format("0{0}{1}0{2}", format.FeetSymbol, format.FeetInchesSeparator, format.InchesSymbol));
                }
                if (format.SupressZeroInches)
                {
                    return(string.Format("{0}{1}", feet, format.FeetSymbol));
                }

                return(string.Format("{0}{1}{2}0{3}", feet, format.FeetSymbol, format.FeetInchesSeparator, format.InchesSymbol));
            }

            string inchesDec = inches.ToString(DecimalNumberFormat(format), numberFormat);

            if (feet == 0)
            {
                if (format.SupressZeroFeet)
                {
                    return(string.Format("{0}{1}", inches, format.InchesSymbol));
                }

                return(string.Format("0{0}{1}{2}{3}", format.FeetSymbol, format.FeetInchesSeparator, inchesDec, format.InchesSymbol));
            }
            return(string.Format("{0}{1}{2}{3}{4}", feet, format.FeetSymbol, format.FeetInchesSeparator, inchesDec, format.InchesSymbol));
        }
Ejemplo n.º 7
0
        public static string ToArchitectural(double length, UnitStyleFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            int    feet      = (int)(length / 12);
            double inchesDec = length - 12 * feet;
            int    inches    = (int)inchesDec;

            if (MathHelper.IsZero(inchesDec))
            {
                if (feet == 0)
                {
                    if (format.SupressZeroFeet)
                    {
                        return(string.Format("0{0}", format.InchesSymbol));
                    }
                    if (format.SupressZeroInches)
                    {
                        return(string.Format("0{0}", format.FeetSymbol));
                    }

                    return(string.Format("0{0}{1}0{2}", format.FeetSymbol, format.FeetInchesSeparator, format.InchesSymbol));
                }
                if (format.SupressZeroInches)
                {
                    return(string.Format("{0}{1}", feet, format.FeetSymbol));
                }

                return(string.Format("{0}{1}{2}0{3}", feet, format.FeetSymbol, format.FeetInchesSeparator, format.InchesSymbol));
            }

            int numerator;
            int denominator;

            GetFraction(inchesDec, (short)Math.Pow(2, format.LinearDecimalPlaces), out numerator, out denominator);

            if (numerator == 0)
            {
                if (inches == 0)
                {
                    if (feet == 0)
                    {
                        if (format.SupressZeroFeet)
                        {
                            return(string.Format("0{0}", format.InchesSymbol));
                        }
                        if (format.SupressZeroInches)
                        {
                            return(string.Format("0{0}", format.FeetSymbol));
                        }

                        return(string.Format("0{0}{1}0{2}", format.FeetSymbol, format.FeetInchesSeparator, format.InchesSymbol));
                    }
                    if (format.SupressZeroInches)
                    {
                        return(string.Format("{0}{1}", feet, format.FeetSymbol));
                    }

                    return(string.Format("{0}{1}{2}0{3}", feet, format.FeetSymbol, format.FeetInchesSeparator, format.InchesSymbol));
                }
                if (feet == 0)
                {
                    if (format.SupressZeroFeet)
                    {
                        return(string.Format("{0}{1}", inches, format.InchesSymbol));
                    }

                    return(string.Format("0{0}{1}{2}{3}", format.FeetSymbol, format.FeetInchesSeparator, inches, format.InchesSymbol));
                }

                return(string.Format("{0}{1}{2}{3}{4}", feet, format.FeetSymbol, format.FeetInchesSeparator, inches, format.InchesSymbol));
            }

            string text = string.Empty;
            string feetStr;

            if (format.SupressZeroFeet && feet == 0)
            {
                feetStr = string.Empty;
            }
            else
            {
                feetStr = feet + format.FeetSymbol + format.FeetInchesSeparator;
            }
            switch (format.FractionType)
            {
            case FractionFormatType.Diagonal:
                text = "\\A1;" + feetStr + inches + "{\\H" + format.FractionHeightScale + "x;\\S" + numerator + "#" + denominator + ";}" + format.InchesSymbol;
                break;

            case FractionFormatType.Horizontal:
                text = "\\A1;" + feetStr + inches + "{\\H" + format.FractionHeightScale + "x;\\S" + numerator + "/" + denominator + ";}" + format.InchesSymbol;
                break;

            case FractionFormatType.NotStacked:
                text = feetStr + inches + " " + numerator + "/" + denominator + format.InchesSymbol;
                break;
            }
            return(text);
        }