Click or drag to resize

DistanceTypeFormat Method

Formats the value into a human friendly string with a unit specification.

Namespace:  KSPDev.GUIUtils.TypeFormatters
Assembly:  KSPDev_Utils.1.0 (in KSPDev_Utils.1.0.dll) Version: 1.0 for KSP v1.6+
Syntax
C#
public static string Format(
	double value,
	Nullable<double> scale = null,
	string format = null
)
Request Example View Source

Parameters

value
Type: SystemDouble
The unscaled numeric value to format.
scale (Optional)
Type: SystemNullableDouble
The fixed scale to apply to the value before formatting. The formatting method can uderstand only a few scales:
  • Meters: scale=1.0. It's a base distance unit in the game.
  • Kilometers: scale=1.0e+3.

The unknown scales will be rounded down to the closest known scale. If this parameter is omitted, then the best scale for the value will be choosen automatically.

format (Optional)
Type: SystemString
The specific numeric number format to use. If this parameter is specified, then the method doesn't try to guess the right scale. Instead, it uses either the provided scale, or 1.0 if nothing is provided. If the format is not specified, then it's choosen basing on the scale.

Return Value

Type: String
A formatted and localized string
Remarks
The method tries to keep the resulted string meaningful and as short as possible. For this reason the big values may be scaled down and/or rounded.
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