Mudassar Majgaonkar
Mudassar Majgaonkar

Reputation: 31

Removing punctuation from given string by retaining ZERO in perl

I am trying to remove punctuations from a given string in perl using following command

      perl -le '$x=19.10 ; $x=~s/[[:punct:]]//g; print $x'

This gives output as : 191

It removes trailing 0 from the input. I want to retain it too.

For other numbers its successfully returns all the values like:

      perl -le '$x=19.11 ; $x=~s/[[:punct:]]//g; print $x'

This gives output as : 1911

How do I retain the trailing 0 in the first case?

Upvotes: 1

Views: 79

Answers (1)

ikegami
ikegami

Reputation: 385897

You assigned the number 19.10000000000000142108547152020037174224854 (the closest floating point number to nineteen and one-tenth) to $x. You then proceeded to stringify that number, giving the string 19.1. From that, you removed the . giving 191.

You could use

my $x = "19.10"; $x =~ s/[[:punct:]]//g;

or

my $x = 19.10; $x *= 100;

Upvotes: 3

Related Questions