Skip to content

DecimalFormatter#1

Open
alfredbaudisch wants to merge 8 commits intopeachey2k2:masterfrom
alfredbaudisch:formatter
Open

DecimalFormatter#1
alfredbaudisch wants to merge 8 commits intopeachey2k2:masterfrom
alfredbaudisch:formatter

Conversation

@alfredbaudisch
Copy link
Copy Markdown

@alfredbaudisch alfredbaudisch commented Nov 25, 2025

A customizable formatter for Decimal, with support for abbreviations, full names, separators and even the possibility of displaying the all zeroes.

Includes class docs and a benchmark against a version of the formatter in GDScript.

Some features (from the DecimalFormatter class docs):

DecimalFormatter provides flexible formatting options for [Decimal] numbers, including:
• Abbreviated format with short (K, M, B) or long (Thousand, Million, Billion) names
• Full number format with thousands and decimal separators
• Automatic formatting based on a threshold
• Full number representation with all zeroes
• Scientific notation for extremely large numbers (beyond 999c)

The formatter supports all standard number abbreviations from Thousand (K) up to Unvigintillion (c). For numbers beyond 999c, it automatically switches to scientific notation for performance.
[codeblocks][gdscript]
var formatter = DecimalFormatter.new()
var number = Decimal.from_float(1234567.89)

# Format with abbreviation
formatter.abbreviation_type = DecimalFormatter.ABBREVIATION_TYPE_SHORT
print(formatter.format(number))  # "1.2 M"

# Format with full number
formatter.format_mode = DecimalFormatter.FORMAT_MODE_FULL
print(formatter.format(number))  # "1,234,567.89"

# Get full number with all zeroes
var big_number = Decimal.from_parts(4.3, 32)
print(formatter.format_full_with_zeroes(big_number))  # "430000000000000000000000000000000"

Example GDScript:

var d1 := Decimal.from_float(1004e65)

var formatter = DecimalFormatter.new()
formatter.abbreviation_type = DecimalFormatter.ABBREVIATION_TYPE_SHORT
formatter.decimal_places = 2  # Show 2 decimal places (e.g., 4.10M)

print(formatter.format(d1))
# Result: 100.40c

print(formatter.format_full_with_zeroes(d1))
# Result: 100,400,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

var d2 := Decimal.from_float(1234)
formatter.threshold = Decimal.from_float(10000)
formatter.decimal_places = 2  # Show 2 decimal places (e.g., 4.10M)
formatter.digits_thousands_separator_threshold = 3
print(formatter.format(d2))
# 1,234

formatter.threshold = Decimal.from_float(1000)
print(formatter.format(d2))
# 1.23K

formatter.decimal_places = 1
print(formatter.format(d2))
# 1.2K

@peachey2k2
Copy link
Copy Markdown
Owner

Thanks for the contribution. This looks pretty good, though it'd be nice to also have support for customized abbreviations for localization purposes, and to have more specialized notations (for logarithmic expressions etc.). Kinda like what Antimatter Dimensions does.

also ignore the failed check, it just failed to push to the binary repo

@alfredbaudisch
Copy link
Copy Markdown
Author

Hey, thanks for the review!

and to have more specialized notations (for logarithmic expressions etc.). Kinda like what Antimatter Dimensions does.

Those are pretty cool! Unfortunately I cannot promise that I'm going to implement this, as for now what I needed was general formatting like I implemented (and mostly inspired by https://crusaders-of-the-lost-idols.fandom.com/wiki/Large_Number_Abbreviations) and adding modular notations like that is way out of scope for my original intended usage :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants