Max
Max

Reputation: 2552

Converting a parameter to a floating point number in AWK

I have this code that works fine:

time1=23245321;
ratio=0.9761;
time1=int(time1*ratio);

but I get it not to work when I transform the 'ratio' variable to a parameter passed to the script with the -v option - time1 results to be equal to 0 (zero).

awk -f script.awk -v ratio=0.9761

It seems ratio is no longer treated as a float. How can I solve this problem?

Upvotes: 0

Views: 122

Answers (2)

Max
Max

Reputation: 2552

It was just a 'locale' issue: I should have called the script using

awk -f script.awk -v ratio=0,9761

because the comma is the decimal separator on my laptop

Upvotes: 1

tommy.carstensen
tommy.carstensen

Reputation: 9622

This works fine on my machine:

awk -v ratio=0.9761 '{
time1=23245321;
time1=int(time1*ratio);
print ratio,time1}' <(echo "Hello world!")

It returns:

0.9761 22689757

Upvotes: 1

Related Questions