Created
March 16, 2025 20:11
-
-
Save tinkerer-red/71e5e2e82a1d2eba16dfd2a61a5956b2 to your computer and use it in GitHub Desktop.
Human Readable Integer
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
function string_format_human_readable(_num) { | |
if (_num == 0) return "zero"; | |
static __ones = [ | |
"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", | |
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", | |
"seventeen", "eighteen", "nineteen" | |
]; | |
static __tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]; | |
//negative | |
if (_num < 0) return $"negative {string_format_human_readable(-_num)}"; | |
// just get the best fitting name | |
if (_num < 20) { | |
return __ones[_num]; | |
} | |
// get the best pair of names (exclude 0 and ten as their included in the previous) | |
if (_num < 100) { | |
var _header = __tens[_num div 10]; | |
var _body = (_num mod 10 != 0) ? $" {__ones[_num mod 10]}" : ""; | |
return _header + _body; | |
} | |
// hundred | |
if (_num < 1000) { | |
var _header = $"{__ones[_num div 100]} hundred"; | |
var _body = _num mod 100; | |
return (_body != 0) ? $"{_header} {string_format_human_readable(_body)}" : _header; | |
} | |
// thousand | |
if (_num < 1000000) { | |
var _header = $"{string_format_human_readable(_num div 1000)} thousand"; | |
var _body = _num mod 1000; | |
return (_body != 0) ? $"{_header} {string_format_human_readable(_body)}" : _header; | |
} | |
// mill | |
if (_num < 1000000000) { | |
var _header = string_format_human_readable(_num div 1000000) + " million"; | |
var _body = _num mod 1000000; | |
return (_body != 0) ? _header + " " + string_format_human_readable(_body) : _header; | |
} | |
//bill | |
var _header = string_format_human_readable(_num div 1000000000) + " billion"; | |
var _body = _num mod 1000000000; | |
return (_body != 0) ? _header + " " + string_format_human_readable(_body) : _header; | |
//in not doing anymore | |
} | |
///example | |
/* | |
for (var i=-100; i<=1000; i++) { | |
show_debug_message($"{i} --> {string_format_human_readable(i)}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment