From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert numbers to string with optimal number of digits?

Solved!
Go to solution

Hi.

 

I am trying to do something that seems quite simply, and I am surprised at how much trouble I am having with this.  I am pretty sure that there is a way to get LabVIEW to do this.

 

I have an array of numbers (double), and I am using the "Array To Spreadsheet String" function to generate a string that I can write to a file.

 

The problem I am having is that I want to string to omit unnecessary trailing zeros after the decimal point, but I don't want to limit the precision of numbers that do require it.

 

For example, if my data is this:

0.02

0.4476

-0.0244570001

 

I would like for the string that I get to be exactly like what I wrote above.  However, depeding on the format string I use, this is what I get:

 

format string: %.10f

0.0200000000

0.4476000000

-0.0244570001

 

<OR>

 

format string: %f  (this seems to default to 6 digits)

0.020000

0.447600

-0.024457

 

<OR>

format string: %.4f

0.0200

0.4476

-0.0244 <= all trailing digits for this number have been lost!!

 

Does someone know what the format string should be to get the string I want (which is this:)

0.02

0.4476

-0.0244570001

 

Thanks in advance,

 

Alejandro

0 Kudos
Message 1 of 4
(2,986 Views)
Solution
Accepted by topic author AlejandroZepeda

Hello Alejandro:

 

Try %#f

 

 

Message 2 of 4
(2,979 Views)

AlejandroZepeda wrote:

Hi.

 

I am trying to do something that seems quite simply,


 

For example, if my data is this:

0.02

0.4476

-0.0244570001

 

I would like for the string that I get to be exactly like what I wrote above.  However, depeding on the format string I use, this is what I get:

 

format string: %.10f

0.0200000000

0.4476000000

-0.0244570001

 

Does someone know what the format string should be to get the string I want (which is this:)

0.02

0.4476

-0.0244570001

 

Thanks in advance,

 

Alejandro


Hi Alejandro,

 

remember that if  you used a %20f formatter you most likely would see a digit appearing in the .4476 value. Your computer is binary, but your display is decimal, there allways will be rounding errors.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 3 of 4
(2,960 Views)

Hello.

 

Thanks for your responses.

 

I tried using %#f, but I got the same result as %f.

Then I tried %#.10f, and that worked exactly as I wanted it to.

 

Thanks a lot for the tip, DonPogi!

 

Alejandro

0 Kudos
Message 4 of 4
(2,925 Views)