ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Number to String syntax - padding zeros keeping decimal values

Solved!
Go to solution

Hello team, 

I am trying get a number, pad it with up to 3 zeros on the left and keep two decimal points of it. 

ie: if the number is 6.25 i want it to be string "006.25"

if it is 60.125 i want it to be "060.12" (round the decimal to nearest hundredth)

 

I tried using the "Format Into String" with differnet variations of "%03d" and ".2f". but i cannot seem to have a the right combination of both of them in one command. Am I missing something here?

 

Using LV2011

 

Thanks  

0 Kudos
Message 1 of 7
(6,496 Views)
Solution
Accepted by Klimovitsky

Have you tried %06.2f?

Message 2 of 7
(6,492 Views)

Joseph has the right idea, but it should be %006.2f as the format string.

 

I tried %06.2f again and see it actually does work.

Message 3 of 7
(6,489 Views)

Actually Joseph Loo got it right. 

 

Many thanks, I was thinking inside the box 😞

 

Cheers 🙂 and thanks once again

0 Kudos
Message 4 of 7
(6,486 Views)

000.2f does not add the zeros before the number. 

0 Kudos
Message 5 of 7
(6,484 Views)

@Klimovitsky wrote:

000.2f does not add the zeros before the number. 



This is expected behavior, in %06.2f the 6 is the minimum field width, The 0 says to pad with 0 to the left to get the minimum field width and .2f specifies to use 2 digits in the decimal part. In %000.2f the minimum field width is 0 so the result will be your number with 2 decimal digits (no padding to the left is necessary).

 

When you use %06.2f with 6.25 you have: 2 decimal digit, the decimal point and number 6, this correspond to a width of 4. So to get to the minimum field width of 6 two zeros are added to the left.

 

Ben64

Message 6 of 7
(6,473 Views)

Thats what I thought too 🙂 

 

Thanks

0 Kudos
Message 7 of 7
(6,471 Views)