← Pixel Dreamer Studio

Show a percent that never says 0% or 100% too early

Show 0% only at zero and 100% only when full, and clamp everything in between to 1 to 99.

int() rounds down, so a character with 9 health out of 1,000 shows 0% and looks dead. roundi() also rounds up, so 995 out of 1,000 shows 100% and the bar looks full. In our Godot 4.7 test over every value from 0 to 1,000, int() was wrong for 9 values and roundi() for 9. Return 0% and 100% only for the real empty and full, and clamp the rest to 1 to 99%: 0 wrong.

The code

Set your Label's text to percent_text(value, max_value) whenever the value changes. It works for health, cultivation progress, loading or anything else with a bar.

extends Node
## Percent text for health, cultivation or loading bars that never shows 0% while
## something is left, or 100% before the bar is really full.


static func percent_text(value: float, max_value: float) -> String:
	if max_value <= 0.0 or value <= 0.0:
		return "0%"
	if value >= max_value:
		return "100%"
	var p := value / max_value * 100.0
	return "%d%%" % clampi(roundi(p), 1, 99)  # 1 to 99 while in between

What we measured

Every whole value from 0 to 1,000 with a maximum of 1,000. Two runs with the same numbers.

Why it happens

A whole-number percent has 101 steps for a bar that may have 1,000. Near the ends, many values share a step, and the two end steps, 0% and 100%, mean something special to the player: dead, or done. Keeping them for the exact empty and full values, and squeezing everything else into 1 to 99, makes the text agree with what the player expects.

What to change

Window frames that stretch the right way: nine-slice window corners. The same problem in a countdown: a timer that shows 0 before time is up.

Free bars and frames: Xianxia UI & Formation Circles has 8 window styles, 24 formation circles, bars, HUD slots and realm badges, every PNG also at @2x, with Godot 4 themes.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Xianxia UI & Formation Circles has bars, HUD slots, 8 window styles, 24 formation circles and realm badges, every PNG also at @2x, with Godot 4 themes.

More how-tos: UI and HUD