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)
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 ;).
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.
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.
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
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.