LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

regular expression to remove trailing zeros

Solved!
Go to solution
I need a regular expression to remove trailing zeros after the decimal point. I tried (?<=\.\d+?)0+(?=\D|$) but I get a error about look behind not a fixed length or something like that. I am not a regex expert and I was wondering just how to do this with regular expression or some other way.
0 Kudos
Message 1 of 16
(10,724 Views)

Hi

 

did you try "%#_6g"? where the 6 is the significant digits. you can change it to your need.

 

try this "%#.6g" if you want 6 digits of precision.

 

how are you trrying to set the format? using a property node? if you want to do it manually on a control object, you can set it in the properties dialog box of the control.

Regards
Freelance_LV
TestAutomation Consultant
Message 2 of 16
(10,720 Views)

Check out this website:

www.regular-expressions.info

>

"There is a God shaped vacuum in the heart of every man which cannot be filled by any created thing, but only by God, the Creator, made known through Jesus." - Blaise Pascal
0 Kudos
Message 3 of 16
(10,700 Views)
Solution
Accepted by Z.K.

 

Z.K. wrote:

 

[...] or some other way.


 

 

 Example_VI.png

 

 

I tried and I tried but I couldn't crack it with one regular expression, so I took the easy way out.  The first Match Pattern finds the decimal point and the second one clears trailing zeros from the rest.  It doesn't discriminate between numbers and everything else, though.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 4 of 16
(10,688 Views)

@jcarmody wrote:

 

@Z.K. wrote:

 

[...] or some other way.


 

 

 Example_VI.png

 

 

I tried and I tried but I couldn't crack it with one regular expression, so I took the easy way out.  The first Match Pattern finds the decimal point and the second one clears trailing zeros from the rest.  It doesn't discriminate between numbers and everything else, though.


Smiley Surprised

 

Well, if Jim can't crack it with a regex, abandon all hope ye who entereth here.

Smiley Surprised

 

Im more of the opinion that a Scan from string to DBL + formating the indicator's display is the better approach though.


"Should be" isn't "Is" -Jay
0 Kudos
Message 5 of 16
(10,680 Views)

Jim,

 

I checked it and it actually works with just one regular expression, 0*$ or 0+$. Before substring is the number without trailing zeros.

Message 6 of 16
(10,660 Views)

@Sergey Kolbunov wrote:

Jim,

 

I checked it and it actually works with just one regular expression, 0*$ or 0+$. Before substring is the number without trailing zeros.



If you can GUARANTEE that a decimal point is in the input string it would work, but try it with an input string of "1000" (without the quotes)

 

Rod.

 

Message 7 of 16
(10,651 Views)

@Jeff Bohrer wrote:

 

[...]

Smiley Surprised

 

Well, if Jim can't crack it with a regex, abandon all hope ye who entereth here.

Smiley Surprised

 

[...]



Darin.K hasn't chimed in.  His opinion is a much better guide as to whether it's solveable or not.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 8 of 16
(10,624 Views)

Well, since you asked my quick answer is to not look at which parts to grab from the string, but rather which parts to eliminate.  I would go with a Search & Replace:

 

RemoveTrailingZeros.png

 

My usual taste is to leave at least one zero after the radix point (ie. 1000.00000 becomes 1000.0), so what I show here uses a minimum width.  The example as written gives a minimum width of three digits after the radix point (if the original number has more than three).  You can change the number 3 to be any minimum width you like, or remove the quantifier for a min width of 1. (\d{1,3}+ becomes \d).

 

Edit:  Added bonus, as you see it will do the deed to multiple numbers in a string without a loop. 

Edit 2:  Would I really do this using a Regex, maybe not, but the point here is that it could be done.

Message 9 of 16
(10,616 Views)

@jcarmody wrote:

@Jeff Bohrer wrote:

 

[...]

Smiley Surprised

 

Well, if Jim can't crack it with a regex, abandon all hope ye who entereth here.

Smiley Surprised

 

[...]



Darin.K hasn't chimed in.  His opinion is a much better guide as to whether it's solveable or not.


Now that Dr K has chimed in... (and solved it without a regex) I'll stand by my statement of "abandon all hope".  some other method may work.  a Regex will not be the best solution in this case.


"Should be" isn't "Is" -Jay
0 Kudos
Message 10 of 16
(10,589 Views)