![]() |
Forum Index : Microcontroller and PC projects : STR$ problem....
Author | Message | ||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9589 |
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? Smoke makes things work. When the smoke gets out, it stops! |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1982 |
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: AustraliaPosts: 6269 |
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. Jim VK7JH MMedit |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4036 |
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. John |
||||
MM_Wombat Senior Member ![]() Joined: 12/12/2011 Location: AustraliaPosts: 139 |
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: AustraliaPosts: 139 |
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 ZealandPosts: 9589 |
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. Smoke makes things work. When the smoke gets out, it stops! |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |