2 个变量的乘法
假设我正在我的程序中研究我的选择之一。 我有一个文本文件,其中包含以下内容。
格式为 Title:Author:Price:QtyAvailable:QtySold
,例如:
Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
下面是我的函数:
printf "%-22s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price","Qty Avail.", "Qty Sold", "Total Sales"
grep "$1" BookDB.txt | while IFS=: read Title Author Price QtyAvailable QtySold; do
printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f\n", "$Title" "$Author" "$Price" "$QtyAvailable" "$QtySold"
done
问题是我需要另一列称为“总销售额”的列,通过乘以 Price
计算得出> 和售出数量
。但是,我尝试了不同的方法,例如 $Price * $QtySold
或 $3 * $5
但程序仍然无法为我计算出来。解决这个问题的正确方法或途径应该是什么?
Supposingly I am working on one of my options in my program.
I have a text file which has the below content.
The format is Title:Author:Price:QtyAvailable:QtySold
, e.g.:
Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
Below is my function:
printf "%-22s %-16s %-14s %-15s %-13s %s\n", "Title", "Author", "Price","Qty Avail.", "Qty Sold", "Total Sales"
grep "$1" BookDB.txt | while IFS=: read Title Author Price QtyAvailable QtySold; do
printf "%-22s %-16s %-14.2f %-15d %-13d %0.2f\n", "$Title" "$Author" "$Price" "$QtyAvailable" "$QtySold"
done
The problem is I need another column which is called Total Sales, computed by multiplying Price
and QtySold
. However I have tried different ways such as $Price * $QtySold
or $3 * $5
but still the program does not compute it out for me. Which should be the correct method or way to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 (())
示例:
对于浮点数,您应该使用 awk 或bc:
但是,对整个脚本使用 awk 更能满足您的需求:
或者如果您希望使用 perl,它会更短一些:
use (())
Example:
For floating point numbers you should use awk or bc:
However, using awk for the entire script is better for your needs:
Or if you wish perl, which is a bit shorter:
line=指环王:Johnny Dept:56.80:100:38
输出:
$3*$5=2158.4
line=Lord of The Ring:Johnny Dept:56.80:100:38
output:
$3*$5=2158.4
你可以像这样使用 bc :
You could use bc like this: