On 2018-07-08 12:37, Dave Howorth wrote:
On Sun, 8 Jul 2018 08:30:01 +0300 Andrei Borzenkov <arvidjaar@gmail.com> wrote:
08.07.2018 02:33, Bernhard Voelker пишет:
On 07/07/2018 02:50 PM, Carlos E. R. wrote:
Hi,
Do we have something that would round a number in a script?
I have:
case "$1" in 72.009|72) DPI=72 ;; 86.0044|86) DPI=86 ;; 96.01) DPI=96 ;; 240.005|240) DPI=240 ;; 149.99) DPI=150 ;; 265.99|266) DPI=266 ;; 299.999|300) DPI=300 ;; 265.989|266) DPI=266 ;; 359.99|359.994|360) DPI=360 ;; 359.99000000000000909) DPI=360 ;;
This is absurd. Now I have another value, 265.99000000000000909 and I have to add another exception. I think I can write up my own tool, but perhaps it already exists :-?
numfmt --round=nearest --format='%.0f'
Which is also locale dependent
bor@bor-Latitude-E5450:~$ numfmt --round=nearest --format='%.0f' 3.7 numfmt: неверный суффикс в входных данных: «3.7» bor@bor-Latitude-E5450:~$ numfmt --round=nearest --format='%.0f' 3,7 4 bor@bor-Latitude-E5450:~$
Isn't that what you would want though? If your locale uses comma for decimal separator, then presumably the argument $1 will be in that format and be processed correctly?
Provided all the software does that. In my case, the strange numbers are generated by another program, identify: set -- $(identify -units PixelsPerInch -format "%x %y %z" "$FILE") which finds out the DPI resolution of a png picture, producing the numbers you can see further up in the quote, using dots for decimal separation while the locale is a comma. To tell the truth, as a programmer I would not know how to write numbers using the locale. For example in Pascal, which is what I use, I would simply use "write(number)" and that would be all. A numeric constant would be "1.234", while in this country it should be "1,234". No provision for locales. Similarly in C. Even worse for reading from input. As a kid, I grew up writing decimal comma. When the first calculators arrived, I switched to dots. I don't remember the teachers complaining if we used either. I would be happy to switch to dots for ever, less problems. It is a nuisance when pasting numbers from texts produced somewhere else into Calc when using the "correct" locale. -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)