简体   繁体   English

AWK + gsub - 如何舍入浮点数

[英]AWK + gsub - how to round floating number

Do you have an idea of how I can round float numbers after multiplying?您知道如何在乘法后舍入浮点数吗?

I have the following SQL dump:我有以下 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');

And I want to multiply the price value (112.50) by 1.21 (taxes) and the round-up or down.我想将价格值 (112.50) 乘以 1.21(税)并向上或向下取整。 I wrote the following command which does the multiplication but I do not know how to round it:我编写了以下执行乘法的命令,但我不知道如何舍入它:

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

The result:结果:

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');

I would like to have there 136 instead of 136.125.我想要 136 而不是 136.125。 Of course, 137 if it would be 136.555.当然,如果是 136.555,则为 137。

Thank you in advance.先感谢您。

This may be what you want:这可能是你想要的:

$ 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');

but the rounding probably won't go quite as you'd like by default.但是默认情况下舍入可能不会像您希望的那样进行。 See 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 for how to control it with GNU awk.请参阅https://www.gnu.org/software/gawk/manual/gawk.html#Round-Functionhttps://www.gnu.org/software/gawk/manual/gawk.html#Setting-the-rounding -mode如何使用 GNU awk 控制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM