How do I use scientific notation for both large and very small numbers in my code?

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

So I’m new and confused.

var gravitational_constant = 6.67e-11
var mass = 1.1e15
var test = 0.000000000667

print(gravitational_constant)
print(6.67e-11)
print(test)
print(mass)
print(1.e15)

and i get this

0
0
0
0
1000000000000000

So from what I gather var is enough to assign a large number, but not using scientific notation? but print() accepts it?
Also how do I assign small float values not even the print function recognises that one?

I have the same problem, I’m looking for change to scientific notation when the number is too long to display in a line.
u_u somebody can help?

SerK | 2020-07-06 09:50

SerK: That’s an entirely different question. I suggest creating a new question for this, rather than commenting on an existing question.

Calinou | 2020-07-06 09:53

You are right.

SerK | 2020-07-06 15:17

:bust_in_silhouette: Reply From: CharlesMerriam

Happened upon your old question.

  1. Godot string formatting is really, really basic. You need to write (or copy) your own routines for adding commas, scientific notation and the like.

  2. The exact maximum for floating point numbers may or may not be platform specific: it is annoyingly hard to tell. The number is low enough that you will need to jump through hoops for the idle game genre (where 1.0e9999 is just another number). For today’s MacOS, any floating point constants greater than 1e512 will give you an error
    built_in_strtod: Exponent too high
    <C++ Source> core/ustring.cpp:2076 @ built_in_strtod()

  3. You can use mine (public domain). Call it like this:

    		% [As.scientific(0.000123), As.scientific(1234.56, 4)])```
     Put the file anywhere in your tree.  I tend to put static utilities under `./util`.
 https://gist.github.com/merriam/587539fb3cd21ac335984599ef4364f6


3.  Have a wonderful day.

Keep hacking!  Keep notes. -- Charles