Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux AWK+;gsub-如何对浮点数进行四舍五入_Linux_Bash_Awk_Gsub - Fatal编程技术网

Linux AWK+;gsub-如何对浮点数进行四舍五入

Linux AWK+;gsub-如何对浮点数进行四舍五入,linux,bash,awk,gsub,Linux,Bash,Awk,Gsub,你知道如何在相乘后对浮点数进行四舍五入吗 我有以下SQL转储: 插入到 `honzavolfcz_产品`(`product_id`、`feed_product_id`、`import_id`、, `导入活动产品“,”型号“,”sku“,”upc“,”ean“,”jan“,”isbn“,”mpn“, `位置“,”数量“,”库存状态“,”产品状态“,”图像“,”, `制造商id`、`shipping`、`price`、`points`、`tax\u class\u id`、, `日期可用、`weig

你知道如何在相乘后对浮点数进行四舍五入吗

我有以下SQL转储:

插入到
`honzavolfcz_产品`(`product_id`、`feed_product_id`、`import_id`、,
`导入活动产品“,”型号“,”sku“,”upc“,”ean“,”jan“,”isbn“,”mpn“,
`位置“,”数量“,”库存状态“,”产品状态“,”图像“,”,
`制造商id`、`shipping`、`price`、`points`、`tax\u class\u id`、,
`日期可用、`weight`、`weight\`class\`id`、`length`、`width`、`height`、,
`长度、类别、id、`subtract`、`minimum`、`sort\u order`、`status`、`date\u added`、,
`日期_修改`,`查看`)
值('10','0','1',',
‘1’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’,
“1”、“1”、“0”、“catalog/zbozi/bozi_laska_obal.jpg”,
'0', '1', '**112.50**', '0', '1',
'2019-01-15', '0.00', '1', '0.00', '0.00', '0.00',
'1', '0', '1', '0', '1', '2019-02-15 16:16:29',
'2019-02-15 16:16:29', '293');
我想把价格值(112.50)乘以1.21(税)和上下取整。我写了下面的命令,它进行乘法运算,但我不知道如何对它进行四舍五入:

awk '{a=substr($58,2,length($58)-3);gsub(a,a*1.21);print}' a > b
结果是:

插入到
`honzavolfcz_产品`(`product_id`、`feed_product_id`、`import_id`、,
`导入活动产品“,”型号“,”sku“,”upc“,”ean“,”jan“,”isbn“,”mpn“,
`位置“,”数量“,”库存状态“,”产品状态“,”图像“,”,
`制造商id`、`shipping`、`price`、`points`、`tax\u class\u id`、,
`日期可用、`weight`、`weight\`class\`id`、`length`、`width`、`height`、,
`长度、类别、id、`subtract`、`minimum`、`sort\u order`、`status`、`date\u added`、,
`日期_修改`,`查看`)
值('10','0','1',',
‘1’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’、‘模型’,
“1”、“1”、“0”、“catalog/zbozi/bozi_laska_obal.jpg”,
'0', '1', '**136.125**', '0', '1',
'2019-01-15', '0.00', '1', '0.00', '0.00', '0.00',
'1', '0', '1', '0', '1', '2019-02-15 16:16:29',
'2019-02-15 16:16:29', '293');
我想要136而不是136.125。当然是137,如果它是136.555


提前谢谢。

这可能是您想要的:

$ awk '{a=substr($58,2); $58=sprintf("\047%d\047,",a*1.21)} 1' file
INSERT INTO honzavolfcz_product (product_id, feed_product_id, import_id, import_active_product, model, sku, upc, ean, jan, isbn, mpn, location, quantity, stock_status_id, product_status_id, image, manufacturer_id, shipping, price, points, tax_class_id, date_available, weight, weight_class_id, length, width, height, length_class_id, subtract, minimum, sort_order, status, date_added, date_modified, viewed) VALUES ('10', '0', '1', '1', 'model', '', '', '', '', '', '', '', '1', '1', '0', 'catalog/zbozi/bozi_laska_obal.jpg', '0', '1', '136', '0', '1', '2019-01-15', '0.00', '1', '0.00', '0.00', '0.00', '1', '0', '1', '0', '1', '2019-02-15 16:16:29', '2019-02-15 16:16:29', '293');

但在默认情况下,舍入可能不会像您希望的那样进行。有关如何使用GNU awk控制它的信息,请参见和。

使用格式字符串这样的功能对您有帮助吗?echo“10.2”| awk'{a=sprintf(%.0f',$1);如果是10.7,则打印a}'。。。echo“10.7”| awk'{a=sprintf(%.0f“,$1);print a}'它打印11不执行
gsub(a,a*1.21)
-想象一下,如果
a
的值出现在输入中的其他位置,而不仅仅是您想要更改的位置,会发生什么。