AWK + gsub - 如何舍入浮点数

AWK + gsub - how to round floating number

你知道我如何在乘法后舍入浮点数吗?

我有以下 SQL 转储:

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', '**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(,2,length()-3);gsub(a,a*1.21);print}' a > b

结果:

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.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(,2); =sprintf("7%d7,",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 控制它的信息,请参阅 https://www.gnu.org/software/gawk/manual/gawk.html#Round-Function and https://www.gnu.org/software/gawk/manual/gawk.html#Setting-the-rounding-mode