Click or drag to resize

DistanceType Class

Localized message formatting class for a numeric value that represents a distance. The resulted message may have a unit specification.
Inheritance Hierarchy
SystemObject
  KSPDev.GUIUtils.TypeFormattersDistanceType

Namespace:  KSPDev.GUIUtils.TypeFormatters
Assembly:  KSPDev_Utils.2.0 (in KSPDev_Utils.2.0.dll) Version: 2.0 for KSP v1.8+
Syntax
C#
public sealed class DistanceType
Request Example View Source

The DistanceType type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleDistanceType
Constructs an object from a numeric value.
Top
Methods
  NameDescription
Public methodStatic memberCode exampleFormat
Formats the value into a human friendly string with a unit specification.
Public methodToString
Returns a string formatted as a human friendly distance specification.
(Overrides ObjectToString.)
Top
Operators
Fields
  NameDescription
Public fieldStatic memberkilometer
Localized suffix for the "kilometer" untis. Scale x1000
Public fieldStatic memberKilometerLocTag
Localization tag for the "kilometer" units.
Public fieldStatic membermeter
Localized suffix for the "meter" units. Scale x1.
Public fieldStatic memberMeterLocTag
Localization tag for the "meter" units.
Public fieldvalue
A wrapped numeric value.
Top
Remarks

Use it as a generic parameter when creating a LocalizableMessage descendants.

The class uses the unit name localizations from the stock module ModuleEnviroSensor. In case of this module is deprecated or the tags are changed, the default English values will be used for the unit names.

Examples
public class DistanceTypeDemo1 : PartModule {
  static readonly Message<DistanceType> msg1 = new Message<DistanceType>(
      "#TypeDemo_msg1", defaultTemplate: "Distance is: <<1>>");

  // Depending on the current language in the system, this method will present different unit names. 
  void Show() {
    Debug.Log(msg1.Format(0.051));
    // Prints: "Distance is: 0.051 m"
    Debug.Log(msg1.Format(0.45));
    // Prints: "Distance is: 0.45 m"
    Debug.Log(msg1.Format(95.45));
    // Prints: "Distance is: 95.5 m"
    Debug.Log(msg1.Format(120.45));
    // Prints: "Distance is: 121 m"
    Debug.Log(msg1.Format(9535.45));
    // Prints: "Distance is: 9536 m"
    Debug.Log(msg1.Format(12345.45));
    // Prints: "Distance is: 12.5 km"
    Debug.Log(msg1.Format(123456.45));
    // Prints: "Distance is: 123457 km"
  }
}
Examples
Debug.Log(DistanceType.Format(0.051));
// Prints: "0.051 m"
Debug.Log(DistanceType.Format(0.45));
// Prints: "0.45 m"
Debug.Log(DistanceType.Format(95.45));
// Prints: "95.5 m"
Debug.Log(DistanceType.Format(120.45));
// Prints: "121 m"
Debug.Log(DistanceType.Format(9535.45));
// Prints: "9536 m"
Debug.Log(DistanceType.Format(12345.45));
// Prints: "12.5 km"
Debug.Log(DistanceType.Format(123456.45));
// Prints: "123457 km"
Examples
Debug.Log(DistanceType.Format(123456.56, scale: 1000));
// Prints: "123.6 km"
Debug.Log(DistanceType.Format(123456.56, scale: 1));
// Prints: "123456.6 m"
Debug.Log(DistanceType.Format(123456.56, scale: 10));
// Scale 10 is not known, so it's rounded down to 1.
// Prints: "123456.6 m"
Debug.Log(DistanceType.Format(123.56, scale: 1000));
// Prints: "0.1 km"
Examples
Debug.Log(DistanceType.Format(1234.5678, format: "0.0000"));
// Prints: "1234.5678 m"
Debug.Log(DistanceType.Format(1234.5678, format: "0.00"));
// Prints: "1234.57 m"
Debug.Log(DistanceType.Format(1234.5678, format: "0.0000", scale: 1000));
// Prints: "1.2346 km"
Debug.Log(DistanceType.Format(1234.5678, format: "0.00", scale: 1000));
// Prints: "1.24 km"
See Also

Reference

KSPDev.GUIUtils.TypeFormattersDistanceType