ksh93 optimises command substitution by not using a pipe or forking when the commands are only builtin commands. Ksh93 was the first Bourne-like shell to support floating point arithmetic. However note that while doubles can represent very large numbers, integers are much more limited. ![]() In zsh (which supports floating point arithmetic (decimal separator is always the period)), you have the rint() math function to give you the nearest integer as a float (like in C) and int() to give you an integer from a float (like in awk). In the case of GNU awk, that's only when $POSIXLY_CORRECT is in the environment. ![]() as decimal radix character (as, is used to separate arguments so couldn't be used in numbers), when taking numbers on input like here from ARGV, some implementations honour the locale's decimal radix character. }' that though literal numbers in the awk language syntax are always using the. Is not POSIX as %f is not required to be supported by POSIX. With yash, you can also do: printf '%.0f' "$(($float))" So, if your floats are always expressed with the period as the decimal separator and you want it to be treated as such by printf regardless of the locale of the user invoking your script, you'd need to fix the locale to C: LC_ALL=C printf '%.0f' "$float" You do get an integer, but chances are that you won't be able to use that integer anywhere.Īlso, as noted by in several printf implementations (bash, ksh93, yash, not zsh, dash or older version of GNU printf), it is affected by the locale (the decimal separator which can be. If you need the result in a variable, you could use command substitution, or the bash specific (though now also supported by zsh): printf -v int %.0f "$float"īut that would remove the fractional part instead of giving you the nearest integer and that wouldn't work for values of $float like 1.2e9 or. ![]() In bash, that's probably as good as it gets.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |