Help converting numbers to strings (Page 1) ● SmileBASIC Source Forums

Register
This website has been moved, please see this post

# Help converting numbers to strings

• #1 ✎ 203 Nathaniel Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements Staff Pick "Your program is one of our favorites!" - Staff Achievements Scholar Received for knowing a great deal about programming topics Achievements 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? Posted
• #2 ✎ 1884 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Try using FORMAT\$() Posted
• #3 ✎ 203 Nathaniel Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements Staff Pick "Your program is one of our favorites!" - Staff Achievements Scholar Received for knowing a great deal about programming topics Achievements OH! It worked! I thought it was going to be much harder than that... Thanks! Posted
• #4 ✎ 1884 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself 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. Posted
• #5 ✎ 1130 snail_ Power User QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! Helper Received for being very helpful around SmileBASIC Source Achievements Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements
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.
Posted
• #6 ✎ 246 niconii Power User Video Games I like to play video games! Hobbies Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies
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.
Posted Edited by niconii
• #7 ✎ 477 SquareFingers
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.
Posted Edited by SquareFingers