Last active
October 8, 2023 10:35
-
-
Save HungryProton/8cdb4930c2006e5f4fe9ad4cf0a2cbea to your computer and use it in GitHub Desktop.
Print large and small numbers with their prefix multipliers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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