Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: forcing output type



> The expression "2/3" is a truncating integer division; its value is
> exactly 0.  Just as in C, if you want to do a floating-point division
> you have to provide floating-point operands.

Here's a hack that lets one do "calc 2/3" and get a floating point
answer.

calc () {
        setopt localoptions extendedglob
        local expr="$*"
        if [[ "$expr" != *.* && "$expr" = *(#b)([0-9]##)(#B)' '#/' '#([0-9]##)* ]]
        then
                expr="${expr/${match[1]}/${match[1]}.0}" 
        fi
        print $(( $expr ))
}



Messages sorted by: Reverse Date, Date, Thread, Author