03-05-2011 09:03 AM
Hi,
I want to convert numbers to strings, but I want that the string takes the number as it is in the memory. For instance if you attach an indicator to a wire with a number, you can see as much decimal digits as you want (up to the machine precision). How can you convert that to a string?
03-05-2011 09:07 AM
The subject should be number to string ofcourse 😄
03-05-2011 09:16 AM
Use the Format into string function.
For the formatter you can use the %#f function. replace # with the number of digits you wish to use.
Ton
03-05-2011 11:29 AM - edited 03-05-2011 11:33 AM
Use type cast. This will convert the number into a binary string where all bits are identical.
(Formatting a number into decimal text as suggested above will never be accurate unless you deal with integers. Even if you specify an infinite number of decimal digits you might no be able to get the original number back in a reverse operation.
This is because many decimal fraction (e.g. 0.2) cannot be fully represented in binary.
So:
03-05-2011 12:06 PM - edited 03-05-2011 12:11 PM
I wrote a parser for basic calculations (+,-,/,*) and it can also work with natural logarithm. The way i programmed it, it first looks for all ln expressions in the string formula, calculates the ln, puts that number back into the formula string and then goes to a parser for the basic calculations, so there are some string to number and number to string in there. Can I use type cast then? Because I can't put a binary string back into the formula. I attached the parser.
03-05-2011 12:07 PM
Some more subVI's
03-05-2011 12:13 PM
Instread of numerous attachments, place all VIs in a zip file. This also keeps them nicely together for the recipient.
Don't forget to say which one is the toplevel VI, especially if the file names are obfuscated (parser, parser2, basic parser, ln parser, etc.). A dyslexic might have a hard time distinguishing them.
Don't be afraid to use longer and clearer file names, you will save significantly more words in the smaller amount of external documentation needed! 😄
03-05-2011 12:34 PM
parser2 is the top VI, I never take much time for elaborate names, since it's for my use/fun only
03-07-2011 08:02 AM
That's a slippery slope. I have seen an awful lot of professionally written code with the same issue. I put good icons and descriptive names on even my test code. It does not take long and saves me a lot of time when I refer to it later. I will admit this is a preference, but it works for long term use.
03-07-2011 09:36 AM
@altenbach wrote:
Use type cast. This will convert the number into a binary string where all bits are identical.
(Formatting a number into decimal text as suggested above will never be accurate unless you deal with integers. Even if you specify an infinite number of decimal digits you might no be able to get the original number back in a reverse operation.
This is because many decimal fraction (e.g. 0.2) cannot be fully represented in binary.
So:
- If you want to format a number as string so it can be read by the average human, use formatting and accept some rounding errors.
- If you need to transfer the data unchanged, but as a string (e.g. for transmission via TCP/IP, storage in a file), use type cast. If you want to convert the string again to a number on the other end, use typecase again with a datatype of the original number.
- If you need to convert your data to a binary string for consumption by a foreign (=non-LabVIEW) program and need to convert the byte order (e.g. to little endian), you would use "flatten to string".
All clear? 🙂See also this post for more details.
I agree with what altenbach said:
There's no point to be able to see a string number with tens of digits with your eyes.