Click or drag to resize

PercentType Class

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

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 PercentType
Request Example View Source

The PercentType type exposes the following members.

Constructors
  NameDescription
Public methodCode examplePercentType
Constructs a precentage type object.
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 percentage specification.
(Overrides ObjectToString.)
Top
Operators
Fields
  NameDescription
Public fieldStatic memberunitName
Suffix for the "percent" units (%).
Public fieldvalue
A wrapped numeric value.
Top
Remarks
Use it as a generic parameter when creating a LocalizableMessage descendants.
Examples
public class PercentTypeDemo1 : PartModule {
  static readonly Message<PercentType> msg1 = new Message<PercentType>(
      "#TypeDemo_msg1", defaultTemplate: "Ratio is: <<1>>");

  // Depending on the current language in the system, this method will present different unit names. 
  void Show() {
    Debug.Log(msg1.Format(0.004));
    // Prints: "Ratio is: 0.40%"
    Debug.Log(msg1.Format(0.0041));
    // Prints: "Ratio is: 0.41%"
    Debug.Log(msg1.Format(0.01));
    // Prints: "Ratio is: 1.0%"
    Debug.Log(msg1.Format(0.014));
    // Prints: "Ratio is: 1.4%"
    Debug.Log(msg1.Format(0.0145));
    // Prints: "Ratio is: 1.45%"
    Debug.Log(msg1.Format(0.01456));
    // Prints: "Ratio is: 1.46%"
    Debug.Log(msg1.Format(0.1));
    // Prints: "Ratio is: 10%"
    Debug.Log(msg1.Format(0.105));
    // Prints: "Ratio is: 11%"
    Debug.Log(msg1.Format(5.5));
    // Prints: "Ratio is: 550%"
  }
}
Examples
Debug.Log(PercentType.Format(0.004));
// Prints: "0.40%"
Debug.Log(PercentType.Format(0.0041));
// Prints: "0.41%"
Debug.Log(PercentType.Format(0.01));
// Prints: "1.0%"
Debug.Log(PercentType.Format(0.014));
// Prints: "1.4%"
Debug.Log(PercentType.Format(0.0145));
// Prints: "1.45%"
Debug.Log(PercentType.Format(0.01456));
// Prints: "1.46%"
Debug.Log(PercentType.Format(0.1));
// Prints: "10%"
Debug.Log(PercentType.Format(0.105));
// Prints: "11%"
Debug.Log(PercentType.Format(5.5));
// Prints: "550%"
Examples
Debug.Log(PercentType.Format(0.12345678, format: "0.0000"));
// Prints: "12.3457%"
Debug.Log(PercentType.Format(0.12345678, format: "0.00"));
// Prints: "12.35%"
See Also

Reference

KSPDev.GUIUtils.TypeFormattersPercentType