FormatNumber

From ZDoom Wiki
Jump to navigation Jump to search

StatusBarCore

native static String FormatNumber(int number, int minsize = 0, int maxsize = 0, int format = 0, String prefix = "")

Usage

Converts a given integer number into a string and pads it with spaces or zeroes so it has a specific width. Normally, this function is to be used in HUDs, but, being static, can technically be called from anywhere as StatusBarCore.FormatNumber or statusbar.FormatNumber (since statusbar is a global pointer to the current HUD).

Parameters

  • int number
The input to format.
  • int minsize
Minimum length of the number in digits. If this is above 0 and number has fewer digits than this, it'll be padded.
For example: if number is 10 and maxsize is 4, the output will be " 10".
  • int maxsize
Maximum length in digits to pad the number to. If this is above 0 and number has more digits than this, it'll be clamped to this value.
For example: if number is 999 and maxsize is 2, the output will be "99".
  • int format
Despite the odd name, this is a bit field for flags. The following flags are available (and can be combined with |):
  • FNF_WHENNOTZERO — if this flag is used and number is 0, the function will return an empty string.
  • FNF_FILLZEROS — the number will be padded with zeroes instead of spaces.
  • String prefix
A string to be put before the formatted number in the final string.

Return value

  • String — returns the formatted string

Examples

Assuming this is called from a BaseStatusBar class (so, CPlayer is a pointer to client player's PlayerInfo):

String healthString = FormatNumber(CPlayer.health, 3, 3, FNF_FILLZEROES, "HP: ");

Possible results: if player's health is 100, healthString is "HP: 100"; if player's health is 45, healthString is "HP: 045", and so on. Due to maxsize being set to 3, the number cannot go above 999.

These examples are from vanilla Doom statusbar's DrawMainBar function:

		DrawString(mHUDFont, FormatNumber(CPlayer.health, 3), (90, 171), DI_TEXT_ALIGN_RIGHT|DI_NOSHADOW);
		DrawString(mHUDFont, FormatNumber(GetArmorAmount(), 3), (221, 171), DI_TEXT_ALIGN_RIGHT|DI_NOSHADOW);

These draw player's armor and health limited to 3 digits (see also DrawString).

See also