简体   繁体   中英

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:

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. 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. Of course, 137 if it would be 136.555.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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