在AWK中将参数转换为浮点数

时间:2022-11-27 17:09:55

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).

但是当我将'ratio'变量转换为使用-v选项传递给脚本的参数时,我认为它不起作用 - time1结果等于0(零)。

awk -f script.awk -v ratio=0.9761

awk -f script.awk -v ratio = 0.9761

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

似乎比率不再被视为浮动。我怎么解决这个问题?

2 个解决方案

#1


1  

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

#2


1  

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

因为逗号是我笔记本电脑上的小数分隔符

#1


1  

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

#2


1  

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

因为逗号是我笔记本电脑上的小数分隔符