LoginLogin

Help converting numbers to strings

Root / Programming Questions / [.]

NathanielCreated:
When you convert an integer held by a variable to a string with STR$() it uses exponents to simplify the number if it's too large. Here's an example in direct mode:
A=1000000
OK
?STR$(A)
1e+06
OK
?STR$(1000000)
1000000
OK
How can I avoid the exponents while still using variables? Or, at the least, what is a function I can define that also works with decimals?

Try using FORMAT$()

OH! It worked! I thought it was going to be much harder than that... Thanks!

Also, the reason 1000000 isn't converted to exponent notation is because it's an integer, while A is a double. You could use an integer variable or convert to an integer using something like A DIV 1, A OR 0, A>>0, etc.

Also, the reason 1000000 isn't converted to exponent notation is because it's an integer, while A is a double. You could use an integer variable or convert to an integer using something like A DIV 1, A OR 0, A>>0, etc.
This is a very annoying feature because it's not even documented and you don't have the choice of turning it off. I'd much rather have a format code for scientific notation, but that's somehow not an option either.

Also, the reason 1000000 isn't converted to exponent notation is because it's an integer, while A is a double. You could use an integer variable or convert to an integer using something like A DIV 1, A OR 0, A>>0, etc.
This is a very annoying feature because it's not even documented and you don't have the choice of turning it off. I'd much rather have a format code for scientific notation, but that's somehow not an option either.
I'm not sure what you mean. It happens because the default type for variables is double. If you name the variable A%, that specifies it as an integer. Alternatively, you can use OPTION DEFINT to make integer variables the default instead, and then doubles can be specified as A#. If you're saying that you never want doubles to be printed in scientific notation, then I see what you mean.

Also, the reason 1000000 isn't converted to exponent notation is because it's an integer,
because when the numeric literal 1000000 is converted to an internal representation of the number, the system uses the integer data type. For properly understanding issues such as this, it's always important to make the distinction between the representation and the thing represented.
while A is a double.
A, unless in program which has OPTION DEFINT (or under other circumstances, e.g. a formal parameter in a DEF), is a variable with the floating-point data type.
You could use an integer variable or convert to an integer using something like A DIV 1, A OR 0, A>>0, etc.
The range of values represented in the floating-point data type is much larger than the range of values represented in the integer data type. This conversion will not only change the data type, it will change the value, if that value is outside the range of the integer data type.