Round/truncate digit in string in zsh (or with external tool)
3
votes
5
answers
4806
views
I'm trying to do an interface to noglob gets away with the quotes!
But, the use of a dot to force floating point calculation is something I'll never remember (you don't use a normal calculator like that). And it is even a bit risky, especially for calculations when it's not obvious that floating point would yield an altogether different result (most likely the one you wanted).
Also, the calculations below show some un-pretty output (the too long real, and the trailing dot).
Perhaps I should combine this (some of it) with the output formatting of @Gille's [answer](https://unix.stackexchange.com/a/66621/13136) below? When I get it to work perfectly, I'll post the result here. (**Edit:** The accepted [answer](https://unix.stackexchange.com/a/66755/13136) works great. Be sure to read the comments to that answer, as well.)
calc () {
echo $(($*));
}
alias calc='noglob calc'
calc 1./3
0.33333333333333331
calc 7.5 - 2.5
5.
bc
so it can be used intuitively and without the annoyance of getting "stuck" in it. I haven't got around to test it that much, because I got stuck on another detail, namely how to present the result (which is, I think, a string).
Rounding or truncating does not matter, either one is fine. Take a look below, and you'll understand immediately. I use zsh but an external tool will be just fine as I won't use this in any time or otherwise critical context, it's just a desktop tool.
calc () {
result=`bc <
Asked by Emanuel Berg
(7101 rep)
Mar 1, 2013, 10:26 PM
Last activity: Dec 30, 2022, 01:21 PM
Last activity: Dec 30, 2022, 01:21 PM