Skip to content

Instantly share code, notes, and snippets.

@HungryProton
Last active October 8, 2023 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HungryProton/8cdb4930c2006e5f4fe9ad4cf0a2cbea to your computer and use it in GitHub Desktop.
Save HungryProton/8cdb4930c2006e5f4fe9ad4cf0a2cbea to your computer and use it in GitHub Desktop.
Print large and small numbers with their prefix multipliers
func format_value(value: float) -> String:
var result := ""
var negative := value < 0.0
value = abs(value)
if _last_value > 1000.0:
var scale = ["K", "M", "G", "T", "P", "E"]
var v = _last_value
var index = -1
while v > 1000.0 and index < (scale.size() - 1):
v /= 1000.0
index += 1
result = String(stepify(v, 0.001)) + scale[index] + unit
if _last_value < 1.0:
var scale = ["m", "u", "n", "a", "p"]
var v = _last_value
var index = -1
while v < 1.0 and index < (scale.size() - 1):
v *= 1000.0
index += 1
result = String(stepify(v, 0.001)) + scale[index] + unit
if negative:
result = "-" + result
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment