Exemplo n.º 1
0
        /// <summary>
        /// Sets country code for this distance this looks up appropriate full name (kilometer or mile)
        /// </summary>
        /// <param name="localeCode">Full locale code(en-US) or only country code (CA) for this distance</param>
        /// <returns>Lower cased full name for distance unit (kilometer or mile)</returns>
        public string LocaleName(string localeCode)
        {
            CountryCode = localeCode;
            switch (SystemType)
            {
            case MeasurementType.Imperial: return(DistanceSpan.Name(this, DistanceType.Mile));

            default:
                return(DistanceSpan.Name(this, DistanceType.Kilometer));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Displays the value of this span.  Use one of the format prefixes to determine output value and format.  See
        /// notes for important formatting details
        /// </summary>
        /// <param name="format">Format argument as provided in the string.Format call</param>
        /// <param name="formatProvider">Format provider as passed in the string.Format call</param>
        /// <returns>Formatted value of this span</returns>
        /// <example>
        /// <para></para>
        /// <para>Uppercase values display entire word.  Lowercase values display abbreviation</para>
        /// <para>{0:^KM} ==> 1.345 kilometers</para>
        /// <para>{0:^km} ==> 1.345 km</para>
        /// <para>{0:^MI} ==> 1.345 miles</para>
        /// <para>{0:^MI} ==> 1.345 mi</para>
        /// <para></para>
        /// <para><b>String Format Codes</b></para>
        /// <para>^(KM,km) kilometers, km</para>
        /// <para>^(M,m)   meters, m</para>
        /// <para>^(CM,cm) centimeters, m</para>
        /// <para>^(MM,mm) millimeters, mm</para>
        /// <para>^(MI,mi) miles, mi.</para>
        /// <para>^(YD,yd) yards, yd.</para>
        /// <para>^(FT,ft) feet, ft.</para>
        /// <para>^(IN,in) inches, in.</para>
        /// <para>^(NM,nm) nautical, nm.</para>
        /// <para></para>
        /// <para>^(Km) return kilometers without unit identifier (e.g. km)</para>
        /// <para>^(Cm) return centimeters without unit identifier</para>
        /// <para>^() no distance span specified but delimiter '^' optionally is.  Return meters</para>
        /// <para>^(Mm) return millimeters without unit identifier</para>
        /// <para>^(Mi) return miles without unit identifier</para>
        /// <para>^(Yd) return yards without unit identifier</para>
        /// <para>^(Ft) return feet without unit identifier</para>
        /// <para>^(In) return Inches without unit identifier</para>
        /// <para>^(Nm) return nautical miles without unit identifer</para>
        /// <para><b>Numerical Formatting</b></para>
        /// <para>You can change the default numerical formatting by supplying .Net standard numerical formatting codes in front of the abbreviation suffix</para>
        /// <para></para>
        /// <para>{0:00#.00K} to display leading zeros => 001.20 kilometers</para>
        /// </example>
        /// <remarks>Formatting codes had to be chosen not to conflict with existing numerical formatting codes</remarks>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (string.IsNullOrEmpty(format) == true)
            {
                return(_meters.ToString(formatProvider));
            }

            string[] aParts = format.Split(new char[] { '^' });
            if (aParts.Length < 2)                                          // distance span delimiter '^' not specified so just use default numeric formatting and return meters
            {
                return(string.Format(formatProvider, format, TotalMeters)); //return value in meters (default)
            }
            string sFormat = "{0";

            if (string.IsNullOrEmpty(aParts[0]) == false)
            {
                sFormat += ":" + aParts[0];
            }
            sFormat += "}";
            switch (aParts[1])
            {
            case "KM": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Kilometer), TotalKilometers));

            case "km": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Kilometer), TotalKilometers));

            case "M": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Meter), TotalMeters));

            case "m": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Meter), TotalMeters));

            case "CM": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Centimeter), TotalCentimeters));

            case "cm": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Centimeter), TotalCentimeters));

            case "MM": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Millimeter), TotalMillimeters));

            case "mm": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Millimeter), TotalMillimeters));

            case "MI": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Mile), TotalMiles));

            case "mi": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Mile), TotalMiles));

            case "YD": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Yard), TotalYards));

            case "yd": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Yard), TotalYards));

            case "FT": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Foot), TotalFeet));

            case "ft": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Foot), TotalFeet));

            case "IN": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.Inch), TotalInches));

            case "in": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.Inch), TotalInches));

            case "NM": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Name(this, DistanceType.MileNautical), TotalNauticalMiles));

            case "nm": return(string.Format(formatProvider, sFormat + " " + DistanceSpan.Abbreviation(DistanceType.MileNautical), TotalNauticalMiles));

            case "Km": return(string.Format(formatProvider, sFormat, TotalKilometers));

            case "": return(string.Format(formatProvider, sFormat, TotalMeters));

            case "Cm": return(string.Format(formatProvider, sFormat, TotalCentimeters));

            case "Mm": return(string.Format(formatProvider, sFormat, TotalMillimeters));

            case "Mi": return(string.Format(formatProvider, sFormat, TotalMiles));

            case "Yd": return(string.Format(formatProvider, sFormat, TotalYards));

            case "Ft": return(string.Format(formatProvider, sFormat, TotalFeet));

            case "In": return(string.Format(formatProvider, sFormat, TotalInches));

            case "Nm": return(string.Format(formatProvider, sFormat, TotalNauticalMiles));
            }
            throw new FormatException(string.Format("Unable to display requested format: '{0}'", format));
        }