[opensuse] rounding a number
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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 :-? - -- Cheers Carlos E. R. (from 42.2 x86_64 "Malachite" at Telcontar) -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iEYEARECAAYFAltAty0ACgkQtTMYHG2NR9VC4gCfV2AUpa7/vHNYhyuv+2IA519i iysAn2TvNKRVw6fYmJGSY13iO191cla+ =r3Vp -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Carlos E. R. wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Hi,
Do we have something that would round a number in a script?
I very rarely had the need, but I think I ended up using printf. -- Per Jessen, Zürich (24.6°C) http://www.hostsuisse.com/ - virtual servers, made in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Op zaterdag 7 juli 2018 14:50:46 CEST schreef Carlos E. R.:
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 :-?
-- Cheers
Carlos E. R. (from 42.2 x86_64 "Malachite" at Telcontar) https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
In the past I used printf "%.0f\n" $NUMBER a lot in scripts -- Gertjan Lettink a.k.a. Knurpht openSUSE Board Member openSUSE Forums Team -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Sat, 07 Jul 2018 15:19:29 +0200 "Knurpht@openSUSE" <knurpht@opensuse.org> wrote:
Op zaterdag 7 juli 2018 14:50:46 CEST schreef Carlos E. R.:
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 :-?
-- Cheers
Carlos E. R. (from 42.2 x86_64 "Malachite" at Telcontar) https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
In the past I used printf "%.0f\n" $NUMBER a lot in scripts
printf -v var "%.*f" 0 6.66; echo $var specifically in your case: printf -v DPI "%.0f" $1 but whenever a bash script starts to do things like this, I switch to perl. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
07.07.2018 16:26, Dave Howorth пишет:
printf -v var "%.*f" 0 6.66; echo $var
This is locale dependant bor@bor-Latitude-E5450:~$ printf "%.*f\n" 0 6.66 bash: printf: 6.66: недопустимое число 0 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-07 15:26, Dave Howorth wrote:
On Sat, 07 Jul 2018 15:19:29 +0200 "Knurpht@openSUSE" <> wrote:
printf -v var "%.*f" 0 6.66; echo $var
specifically in your case: printf -v DPI "%.0f" $1
but whenever a bash script starts to do things like this, I switch to perl.
Ah, but I don't know perl ;-) My script is now happily running with: DPI=`LC_NUMERIC=en_GB.utf8 printf "%.0f\n" $1` -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
On 2018-07-07 15:19, Knurpht@openSUSE wrote:
Op zaterdag 7 juli 2018 14:50:46 CEST schreef Carlos E. R.:
https://bits.mdminhazulhaque.io/linux/round-number-in-bash-script.html
In the past I used printf "%.0f\n" $NUMBER a lot in scripts
Thanks. I tried: #!/bin/bash NUMBER=265.99000000000000909 printf "%.0f\n" $NUMBER but I get: cer@Telcontar:~> bash bin/float_test bin/float_test: line 3: printf: 265.99000000000000909: invalid number 0 cer@Telcontar:~> Mmm? maybe too many digits? The link you posted suggests: printf "%.0f\n" 123.456 But I get an error with that: cer@Telcontar:~> printf "%.0f\n" 123.456 bash: printf: 123.456: invalid number 0 cer@Telcontar:~> Ah, I know why: cer@Telcontar:~> printf "%.0f\n" 123,456 123 cer@Telcontar:~> The locale. Ugh. So... cer@Telcontar:~> LC_NUMERIC=en_GB.utf8 printf "%.0f\n" 123.456 123 cer@Telcontar:~> Thank you both :-) -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
07.07.2018 15:50, Carlos E. R. пишет:
Hi,
Do we have something that would round a number in a script?
This obviously depends on script language (and there are more than one shell and some of them support floating point arithmetic). Off the head bor@bor-Latitude-E5450:~$ dc -e '1.6 0.3 + 1 / p 1.6 0.5 + 1 / p' 1 2 (final deletion due to addition ignoring precision).
On 07/07/18 08:50 AM, Carlos E. R. wrote:
Do we have something that would round a number in a script?
In a Ruby script there is the numeric function 'floor()' that does all of what you describe. I see that it is also available in the POSIX compatibility module for Perl. There is a similar function in Python. The bash shell doesn't, it seems, do floating point arithmetic. of course the older ksh does --- for i in 72.0009 86.0044 96.01 240.005 do ksh93 -c "print $(( floor($i) ))" done ---- OOPS! have to start that in ksh93 anyway as bash upchuks on things like "72.0009". So, we've taken a step backwards with using Bash? -- A: Yes. > Q: Are you sure? >> A: Because it reverses the logical flow of conversation. >>> Q: Why is top posting frowned upon? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Anton Aylward wrote:
On 07/07/18 08:50 AM, Carlos E. R. wrote:
Do we have something that would round a number in a script?
In a Ruby script there is the numeric function 'floor()' that does all of what you describe. I see that it is also available in the POSIX compatibility module for Perl. There is a similar function in Python.
The bash shell doesn't, it seems, do floating point arithmetic. of course the older ksh does
---
for i in 72.0009 86.0044 96.01 240.005 do ksh93 -c "print $(( floor($i) ))" done
----
OOPS! have to start that in ksh93 anyway as bash upchuks on things like "72.0009".
It works fine here: for i in 72.0009 86.0044 96.01 240.005 do printf "%.0f\n" $i done 72 86 96 240 -- Per Jessen, Zürich (25.7°C) http://www.hostsuisse.com/ - virtual servers, made in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-07 18:54, Per Jessen wrote:
Anton Aylward wrote:
On 07/07/18 08:50 AM, Carlos E. R. wrote:
Do we have something that would round a number in a script?
In a Ruby script there is the numeric function 'floor()' that does all of what you describe. I see that it is also available in the POSIX compatibility module for Perl. There is a similar function in Python.
The bash shell doesn't, it seems, do floating point arithmetic. of course the older ksh does
---
for i in 72.0009 86.0044 96.01 240.005 do ksh93 -c "print $(( floor($i) ))" done
----
OOPS! have to start that in ksh93 anyway as bash upchuks on things like "72.0009".
It works fine here:
for i in 72.0009 86.0044 96.01 240.005 do printf "%.0f\n" $i done
72 86 96 240
Only if your locale uses the decimal point. Spain uses the comma as decimal separator, so it doesn't. -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
Carlos E. R. wrote:
On 2018-07-07 18:54, Per Jessen wrote:
Anton Aylward wrote:
On 07/07/18 08:50 AM, Carlos E. R. wrote:
Do we have something that would round a number in a script?
In a Ruby script there is the numeric function 'floor()' that does all of what you describe. I see that it is also available in the POSIX compatibility module for Perl. There is a similar function in Python.
The bash shell doesn't, it seems, do floating point arithmetic. of course the older ksh does
---
for i in 72.0009 86.0044 96.01 240.005 do ksh93 -c "print $(( floor($i) ))" done
----
OOPS! have to start that in ksh93 anyway as bash upchuks on things like "72.0009".
It works fine here:
for i in 72.0009 86.0044 96.01 240.005 do printf "%.0f\n" $i done
72 86 96 240
Only if your locale uses the decimal point. Spain uses the comma as decimal separator, so it doesn't.
Yes, the locale is important, but I assume Anton was using a period as decimal separator too. -- Per Jessen, Zürich (20.5°C) http://www.hostsuisse.com/ - dedicated server rental in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 08/07/18 04:16 AM, Per Jessen wrote:
Yes, the locale is important, but I assume Anton was using a period as decimal separator too.
The point Anton was making, it would appear, is that the BASH shell does not handle floating point number. Even if your locale uses commas as the decimal separator. Whereas the old Korn shell from 25 years ago does. -- A: Yes. > Q: Are you sure? >> A: Because it reverses the logical flow of conversation. >>> Q: Why is top posting frowned upon? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-08 19:04, Anton Aylward wrote:
On 08/07/18 04:16 AM, Per Jessen wrote:
Yes, the locale is important, but I assume Anton was using a period as decimal separator too.
The point Anton was making, it would appear, is that the BASH shell does not handle floating point number. Even if your locale uses commas as the decimal separator.
Whereas the old Korn shell from 25 years ago does.
Not entirely exact. printf is a bash builtin, and it prints floats. :-) Yap, I know, no float math natively. -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
On 08/07/18 01:39 PM, Carlos E. R. wrote:
Yap, I know, no float math natively.
I was delighted when the shell moved on from the original Bourne minimalist of the PDP-11 not-enough-memory days to include such common things as that print formatting, a built-in 'test' and a few other things that were common in scripts. having a built in 'expr' and that concept expanded to deal with arithmetic (such as loop counting!) was wonderful. I wasn't to happy with the bloat that followed for interactive use. But that is the price paid for the one shell for terminal and scripts and being able to type scripts at the command line. But it's not real arithmetic, is it, only integer arithmetic? Other scripting languages could deal with floating point. The basic Linux shell can't, but the Korn shell of 25 years ago could. Now why was that ksh code never fitted on BASH? -- A: Yes. > Q: Are you sure? >> A: Because it reverses the logical flow of conversation. >>> Q: Why is top posting frowned upon? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 07/08/2018 07:39 PM, Carlos E. R. wrote:
On 2018-07-08 19:04, Anton Aylward wrote:
Whereas the old Korn shell from 25 years ago does.
Not entirely exact. printf is a bash builtin, and it prints floats. :-)
Not entirely exact. ;-) Compare: $ printf --version bash: printf: --: invalid option printf: usage: printf [-v var] format [arguments] vs. $ /usr/bin/printf --version | head -n1 printf (GNU coreutils) 8.29 The former is bash's builtin, while the coreutils package makes it available for other environments which just exec("printf"). The projects try to keep the functionality in sync, for obvious reasons. Have a nice day, Berny -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-09 18:01, Bernhard Voelker wrote:
On 07/08/2018 07:39 PM, Carlos E. R. wrote:
On 2018-07-08 19:04, Anton Aylward wrote:
Whereas the old Korn shell from 25 years ago does.
Not entirely exact. printf is a bash builtin, and it prints floats. :-)
Not entirely exact. ;-) Compare:
$ printf --version bash: printf: --: invalid option printf: usage: printf [-v var] format [arguments]
vs.
$ /usr/bin/printf --version | head -n1 printf (GNU coreutils) 8.29
The former is bash's builtin, while the coreutils package makes it available for other environments which just exec("printf"). The projects try to keep the functionality in sync, for obvious reasons.
Yes, it happens for several bash builtins. "test" is another. -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
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' Have fun, Berny
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:~$
Have fun, Berny
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? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
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)
Carlos E. R. wrote:
To tell the truth, as a programmer I would not know how to write numbers using the locale.
man printf :-) "For some numeric conversions a radix character (`decimal point') or thousands' grouping character is used. " Example: int main( int argc, char **argv ) { setlocale( LC_NUMERIC, getenv( "KLOP99" ) ); printf("%'.2f", 1234567.89); } for i in nl_NL.utf8 da_DK.utf8 de_CH.utf8 en_GB.utf8 do KLOP99=$i ./k done 1234567,89 1.234.567,89 1'234'567.89 1,234,567.89 Normally a programmer just needs to be aware that a locale will be applied on input and output, but she should not need to do anything extra. -- Per Jessen, Zürich (23.8°C) http://www.dns24.ch/ - free dynamic DNS, made in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-08 13:26, Per Jessen wrote:
Carlos E. R. wrote:
To tell the truth, as a programmer I would not know how to write numbers using the locale.
man printf :-)
Ah, but I use pascal ;-)
"For some numeric conversions a radix character (`decimal point') or thousands' grouping character is used. "
Example:
int main( int argc, char **argv ) { setlocale( LC_NUMERIC, getenv( "KLOP99" ) ); printf("%'.2f", 1234567.89); }
for i in nl_NL.utf8 da_DK.utf8 de_CH.utf8 en_GB.utf8 do KLOP99=$i ./k done
1234567,89 1.234.567,89 1'234'567.89 1,234,567.89
I don't understand what KLOP99 is :-? Not in man printf. The name of the current program? ... No... Ah, a random environment var that you use to pass parameters to the program "k". For the record, when I studied C and later when I programmed in C (msdos and windows) I never saw a mention of this at all.
Normally a programmer just needs to be aware that a locale will be applied on input and output, but she should not need to do anything extra.
If that language and library set do it that way, and depending on the operating system chosen. -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
Carlos E. R. wrote:
On 2018-07-08 13:26, Per Jessen wrote:
Carlos E. R. wrote:
To tell the truth, as a programmer I would not know how to write numbers using the locale.
man printf :-)
Ah, but I use pascal ;-)
I'm sure pascal has man pages and manuals too. [snip]
For the record, when I studied C and later when I programmed in C (msdos and windows) I never saw a mention of this at all.
Yes, there will be many things and concepts that fit into that, but to many programmers, locale considerations will probably always remain a bit exotic. -- Per Jessen, Zürich (25.2°C) http://www.dns24.ch/ - your free DNS host, made in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-08 14:19, Per Jessen wrote:
Carlos E. R. wrote:
On 2018-07-08 13:26, Per Jessen wrote:
Carlos E. R. wrote:
To tell the truth, as a programmer I would not know how to write numbers using the locale.
man printf :-)
Ah, but I use pascal ;-)
I'm sure pascal has man pages and manuals too.
Sure. I had a quick look on "locale", did not find how to change numeric treatment so fast and so globally. Some of the entries I found apply only to Windows. It handles language localization differently, too.
[snip]
For the record, when I studied C and later when I programmed in C (msdos and windows) I never saw a mention of this at all.
Yes, there will be many things and concepts that fit into that, but to many programmers, locale considerations will probably always remain a bit exotic.
Yep. IN the case that originated my problem, "identify" has no treatment at all. -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
On 07/08/2018 01:26 PM, Per Jessen wrote:
Carlos E. R. wrote:
To tell the truth, as a programmer I would not know how to write numbers using the locale.
man printf :-)
"For some numeric conversions a radix character (`decimal point') or thousands' grouping character is used. "
Example:
int main( int argc, char **argv ) { setlocale( LC_NUMERIC, getenv( "KLOP99" ) ); printf("%'.2f", 1234567.89); }
for i in nl_NL.utf8 da_DK.utf8 de_CH.utf8 en_GB.utf8 do KLOP99=$i ./k done
1234567,89 1.234.567,89 1'234'567.89 1,234,567.89
Normally a programmer just needs to be aware that a locale will be applied on input and output, but she should not need to do anything extra.
Or setting the wanted LC_NUMERIC for the command of choice in the shell: LC_NUMERIC=da_DK.utf8 numfmt ... Have a nice day, Berny -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Bernhard Voelker wrote:
On 07/08/2018 01:26 PM, Per Jessen wrote:
Carlos E. R. wrote:
To tell the truth, as a programmer I would not know how to write numbers using the locale.
man printf :-)
"For some numeric conversions a radix character (`decimal point') or thousands' grouping character is used. "
Example:
int main( int argc, char **argv ) { setlocale( LC_NUMERIC, getenv( "KLOP99" ) ); printf("%'.2f", 1234567.89); }
for i in nl_NL.utf8 da_DK.utf8 de_CH.utf8 en_GB.utf8 do KLOP99=$i ./k done
1234567,89 1.234.567,89 1'234'567.89 1,234,567.89
Normally a programmer just needs to be aware that a locale will be applied on input and output, but she should not need to do anything extra.
Or setting the wanted LC_NUMERIC for the command of choice in the shell:
LC_NUMERIC=da_DK.utf8 numfmt ...
Provided the command does a setlocale(LC_ALL,"") call. Otherwise, the environment is ignored and the locale remains as "C". -- Per Jessen, Zürich (19.1°C) http://www.cloudsuisse.com/ - your owncloud, hosted in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 07/09/2018 08:45 AM, Per Jessen wrote:
Bernhard Voelker wrote:
LC_NUMERIC=da_DK.utf8 numfmt ...
Provided the command does a setlocale(LC_ALL,"") call. Otherwise, the environment is ignored and the locale remains as "C".
sure, numfmt is part of the GNU coreutils, so it adheres to the GNU coding and POSIX standards: https://git.sv.gnu.org/cgit/coreutils.git/tree/src/numfmt.c?id=4397410532#n1... Have a nice day, Berny -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-09 10:54, Bernhard Voelker wrote:
On 07/09/2018 08:45 AM, Per Jessen wrote:
Bernhard Voelker wrote:
LC_NUMERIC=da_DK.utf8 numfmt ...
Provided the command does a setlocale(LC_ALL,"") call. Otherwise, the environment is ignored and the locale remains as "C".
sure, numfmt is part of the GNU coreutils, so it adheres to the GNU coding and POSIX standards:
https://git.sv.gnu.org/cgit/coreutils.git/tree/src/numfmt.c?id=4397410532#n1...
Well, in this part of the thread I asked what to do inside a program to have it support numeric locales, and Per simply gave an example. -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
On 07/09/2018 11:09 AM, Carlos E. R. wrote:
Well, in this part of the thread I asked what to do inside a program to have it support numeric locales, and Per simply gave an example.
ah sorry, I didn't follow every line in the thread. Have a nice day, Berny -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Bernhard Voelker wrote:
On 07/09/2018 08:45 AM, Per Jessen wrote:
Bernhard Voelker wrote:
LC_NUMERIC=da_DK.utf8 numfmt ...
Provided the command does a setlocale(LC_ALL,"") call. Otherwise, the environment is ignored and the locale remains as "C".
sure, numfmt is part of the GNU coreutils, so it adheres to the GNU coding and POSIX standards:
thanks for mentioning that Berny - I was not aware of those. -- Per Jessen, Zürich (28.4°C) http://www.dns24.ch/ - your free DNS host, made in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-07 14:50, 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 :-?
What do you want to achieve and where do these numbers come from? Maybe you could work something out in the prior step or the one after this. Cheers -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On 2018-07-08 12:32, Christian Neyers wrote:
On 2018-07-07 14:50, 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 :-?
What do you want to achieve and where do these numbers come from? Maybe you could work something out in the prior step or the one after this.
No. The numbers are generated by "identify", part of the ImageMagick(1). I can not alter the output. identify - describes the format and characteristics of one or more image files. The code section now is: "image/png") NOMBRE=`basename "$FILE" png` NOMBRECMPLTO=$DIRECTORIO/$NOMBRE echo calculating resolution set -- $(identify -units PixelsPerInch -format "%x %y %z" "$FILE") DPI=`LC_NUMERIC=en_GB.utf8 printf "%.0f\n" $1` and is working fine - at least in this respect :-) -- Cheers / Saludos, Carlos E. R. (from 42.3 x86_64 "Malachite" at Telcontar)
participants (8)
-
Andrei Borzenkov
-
Anton Aylward
-
Bernhard Voelker
-
Carlos E. R.
-
Christian Neyers
-
Dave Howorth
-
Knurpht@openSUSE
-
Per Jessen