Click or drag to resize

MassTypeFormat Method

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

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 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:
  • Tons: scale=1.0. It's a base mass unit in the game.
  • Kilograms: scale=1.0e-3.
  • Grams: scale=1.0e-6.

The unknown scales will be rounded up 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(MassType.Format(1.0));
// Prints: "1.0 t"
Debug.Log(MassType.Format(1.567));
// Prints: "1.57 t"
Debug.Log(MassType.Format(10.0));
// Prints: "10 t"
Debug.Log(MassType.Format(10.56));
// Prints: "10.6 t"
Debug.Log(MassType.Format(100.0));
// Prints: "100 t"
Debug.Log(MassType.Format(100.5));
// Prints: "101 t"

Debug.Log(MassType.Format(0.1234567));
// Prints: "124 kg"
Debug.Log(MassType.Format(0.0123456));
// Prints: "12.4 kg"
Debug.Log(MassType.Format(0.0012345));
// Prints: "1.24 kg"

Debug.Log(MassType.Format(0.0001234567));
// Prints: "124 g"
Debug.Log(MassType.Format(0.0000123456));
// Prints: "12.4 g"
Debug.Log(MassType.Format(0.0000012356));
// Prints: "1.24 g"
Debug.Log(MassType.Format(0.0000001235));
// Prints: "0.124 g"
Examples
Debug.Log(DistanceType.Format(0.12345678, scale: 1));
// Prints: "0.124 t"
Debug.Log(DistanceType.Format(0.12345678, scale: 0.001));
// Prints: "124 kg"
Debug.Log(DistanceType.Format(0.12345678, scale: 0.0001));
// Scale 0.0001, so it's roudned up to 0.001
// Prints: "124 kg"
Debug.Log(DistanceType.Format(0.12345678, scale: 0.000001));
// Prints: "123457 g"
Debug.Log(DistanceType.Format(0.12345678, scale: 0.0000001));
// Scale 0.0000001, so it's roudned up to 0.000001
// Prints: "123457 g"
Examples
Debug.Log(MassType.Format(0.12345678, format: "0.0000"));
// Prints: "0.1235 t"
Debug.Log(MassType.Format(0.12345678, format: "0.00"));
// Prints: "0.12 t"
Debug.Log(MassType.Format(0.12345678, format: "0.0000", scale: 0.001));
// Prints: "123.4568 kg"
Debug.Log(MassType.Format(0.12345678, format: "0.0000", scale: 0.000001));
// Prints: "123456.7800 g"
See Also