Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:29 07 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : STR$ problem....

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9589
Posted: 08:33pm 14 Oct 2014
Copy link to clipboard 
Print this post

Hi folks.

Normally, when you want a string representation of a number, you use the STR$ function thus:

N$=STR$(1234)

This will return "1234" in a string.

In my case, I need to be able to convert a ten-digit number like this, but the uM's floating point is taking over, me thinks.....

N$=STR$(1234567890)

1.23457e+09 is the response, and is totally incorrect.

I need it number-for-number, just like with the smaller 1234 example.
N$="1234567890" in other words.

Anyone know how to get around this issue?
Edited by Grogster 2014-10-16
Smoke makes things work. When the smoke gets out, it stops!
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1982
Posted: 08:57pm 14 Oct 2014
Copy link to clipboard 
Print this post

Hi Grogster,
Can you split the number into two groups ie 12345 and 67890
The join the strings together.
Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 09:00pm 14 Oct 2014
Copy link to clipboard 
Print this post

You are right, the limitations of 32 bit floating point have caught up with you.
STR$ seems to be limited to 6 significant figures which is less than the floating point limit.

It is fairly easy to get a few more significant figures but try entering numbers with too many significant figures and what you enter will not be what you end up with.

do
input "Number ";n

print uFormat$(n)
loop

function uFormat$(x)
if abs(x) > 999999 then
xhi=x\1000000
xlo=abs(x mod 1000000)
uFormat$=str$(xhi)+str$(xlo)
else
uFormat$=str$(x)
endif
end function


works for positive and negative numbers

Best option is to keep the data as a string if possible.

JimEdited by TassyJim 2014-10-16
VK7JH
MMedit
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4036
Posted: 09:10pm 14 Oct 2014
Copy link to clipboard 
Print this post

If you need more precision, try Peter's library.

As Jim has just pointed out, at some number of digits (about 7) a number entered cannot be stored accurately (i.e. some precision is lost) so when printed out you inevitably get something that's not what was entered. This really really is exactly how all fp (floating point) works. It's no different in concept to trying to have one third (1 / 3) or a seventh (1 / 7) written down exactly on paper or with an electronic calculator. On paper we resort to fractions or to those dots that mean "repeating". Can't do those with fp.

Maybe you met the proof that point nine recurring equals 1? Not with fp it doesn't!

However, STR$ also seems to print a little less than it perhaps could. Peter's lib will let you do more.

JohnEdited by JohnS 2014-10-16
 
MM_Wombat
Senior Member

Joined: 12/12/2011
Location: Australia
Posts: 139
Posted: 09:23pm 14 Oct 2014
Copy link to clipboard 
Print this post

Could you just do two five digit numbers and add the text together?

N = 1234567890
temp_HI = int(N/100000)
temp_LO = n mod 100000
N$=str$(temp_HI)+str$(temp_LO)

When I did this, on my CMM, it output, 1234567968, which is not quite the correct answer, lmao

but this idea may spark another !!

AussieWombat
Keep plugging away, it is fun learning
But can be expensive (if you keep blowing things up).

Maximite, ColourMaximite, MM+
 
MM_Wombat
Senior Member

Joined: 12/12/2011
Location: Australia
Posts: 139
Posted: 09:24pm 14 Oct 2014
Copy link to clipboard 
Print this post

beaten to the punch, but on the right tangent..
AussieWombat
Keep plugging away, it is fun learning
But can be expensive (if you keep blowing things up).

Maximite, ColourMaximite, MM+
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9589
Posted: 10:06pm 14 Oct 2014
Copy link to clipboard 
Print this post

Thank you for all the replies, folks.

I wanted to be able to specify a 10-digit cell-phone number, which is passed to a routine that makes use of a cell-phone module to text a message to the phone.

In the sub, I am passing the number as an argument - N.

I think I will just pass it as a string - N$ - and be done with it.

I will try it now, and let the thread know what happens......

EDIT: Yes, that works beautifully, and no complicated mathematics to worry about.Edited by Grogster 2014-10-16
Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025