Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting rid of tailing zeroes in floating number

Status
Not open for further replies.

yesornot

Technical User
Apr 15, 2015
7
PL

$ echo 4000.24320000000000223230000|sed s/0*$//g
4000.2432000000000022323
(ok)

$ echo 4000.0000|sed s/0*$//g
4000.
(not ok - expected 4000)

$ echo 4000|sed s/0*$//g
4
(not ok - expected 4000)
 
ok, this worked
awk '{if($0~/\./){sub("0*$","",$0);sub("\\.$","",$0);}print}'
 
This works. Unfortunately complex because sed -r doesn't seem to support greediness (?) in ERE:

Code:
sed -r 's/(\.([0-9]*[1-9])?)0+$/\1/;s/\.$//'

By contrast, perl does:

Code:
perl -pe 's/(\.[0-9]*?)0+$/\1/;s/\.$//'

But the awk solution is probably the must human-compatible anyway. :)

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top