Scientific notation when the number is too long.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SerK

I am making a calculator with godot to learn gdscript, and I have found a problem, when a number is too long, the display which is a LABEL, is deformed or displaced. I have fixed this, but I would like that if the number is excessively long (either very high or very low) the number was presented in scientific notation with a determined integer length.
Any suggestion?

:bust_in_silhouette: Reply From: DDoop

Set a MAX_DIGITS const at the top of your script like so (however big you like, alternatively use a variable that depends on screen size if you are masochistic and want more work for yourself):
const MAX_DIGITS: int = 10
To only check the whole number section of your value (which is typically how I see scientific notation used in calculators), cast your display value to a string, and call .split(".", allow_empty = false, maxsplit = 1) on it. Assign the returned PoolStringArray value to a variable, and then check the 0th index’s length.

split() splits your display value (which I assume is a float) into 2 elements: the whole value and the decimal [citation]. Calling .length() on the 0th element will tell you how many digits are in your whole number (If you wanted to apply scientific notation to very small numbers, understanding this should make it clear how to do so, except you’d be checking the length of the 1st indexed element as well). Compare that int value representing the length of your whole number to your MAX_DIGITS const in a conditional statement, and if it is greater, use string formatting to get a new display value string like so:
"%sx10^%s" % [formatted_value, exponent] (or %se%s, however you like to see it)
Where formatted_value is your whole number with a "." inserted after the 0th element, and exponent is just whole_number.length() - 1

Just keep in mind that Godot does not have unlimited size for floats/ints. For ints the docs state [-2^63, 2^63 - 1]. I’m not sure what it is for floats, though.
I was a little surprised to find how half-baked support for scientific notation seems, though. You can pass it to the int or float constructor, but as far as I can tell, that’s the only reference the docs make to it.

Thanks, I gonna read tomorrow carefully, with time and try to implement that, after that I tell you how it goes ;).

SerK | 2020-07-07 00:06

Thanks, I think this is beyond my capabilities at the moment.You have given me a good starting point from where I was stuck, I have a lot to think about and a lot of code to change, so I will take it easy, thank you very much for giving me the starting point ;).
The truth is that I have been learning for about a month and it is taking me a lot.

SerK | 2020-07-07 17:11

Every programmer starts learning by floundering. The most frustrating experiences are the ones you will learn the most from if you exercise perseverance. Good luck on your journey.

DDoop | 2020-07-08 16:35

I was a little surprised to find how half-baked support for scientific notation seems, though.

I don’t think games need to use it very often, which probably explains it. That said, if you think it can be made clearer in the documentation, please contribute :slight_smile:

Calinou | 2020-07-09 07:52

I will try to at some point. I’m definitely interested in reading the commits that resulted in constructors having the ability to take scientific notation, I wonder if anyone planned back then to expand the functionality further. I’m still learning about version control systems.

DDoop | 2020-07-09 15:47