godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Add a String method to format numbers with a delimiter separator and custom decimal separator #9289

Open Calinou opened 8 months ago

Calinou commented 8 months ago

Describe the project you are working on

The Godot editor :slightly_smiling_face:

Describe the problem or limitation you are having in your project

Large numbers are currently displayed as-is in the editor, without any separators for thousands. Additionally, projects regularly need to display large numbers for debugging purposes, and readability is important in this scenario.

This is also a common use case for management or idle games which often display large numbers.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

By adding a String.number_format() method (and having TextServer use it internally when relevant), we can improve the display of large numbers in the editor:

Screenshot_20240311_224042 Screenshot_20240311_235852 Screenshot_20240311_235917

TextServer takes the current language into account and aims to use the correct separator for the language. Here's an example with French:

Screenshot_20240312_000354

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

If this enhancement will not be used often, can it be worked around with a few lines of script?

This can be worked around with a script, but not for the editor's own display of large numbers.

Is there a reason why this should be core and not an add-on in the asset library?

This is about improving editor usability as well as quality of life when debugging a project.

charliewhitfield commented 5 months ago

Two questions about this proposal:

  1. ~Does this affect the Output window (i.e., output from print statements)?~ [Edit: sorry, I think I see this in the first image.]
  2. Would I be able to supply my own format_number() function in an editor plugin?

I'm asking because I also have the "small number problem", where the editor prints "0" when the number is neither 0.0 nor approximately 0.0 by is_zero_approx() test. This happens quite frequently in my solar system simulation.

Calinou commented 5 months ago
  1. Does this affect the Output window (i.e., output from print statements)? [Edit: sorry, I think I see this in the first image.]

It wouldn't, but you could use the number formatting method manually within a print() if you wish. The first image is the 3D editor viewport's View Information panel, not the Output bottom panel.

  1. Would I be able to supply my own format_number() function in an editor plugin?

You could choose to use a custom separator for thousands and decimals, as well as choosing the "length" of thousands so to speak. The PR's String.format_number() method provides all three options as optional parameters.

I'm asking because I also have the "small number problem", where the editor prints "0" when the number is neither 0.0 nor approximately 0.0 by is_zero_approx() test. This happens quite frequently in my solar system simulation.

This is a different issue: https://github.com/godotengine/godot/issues/78204