1

To convert from percent to decimal:  divide by 100, and remove the "%" sign.

The snippet (/ PERCENT 100) yields a seemingly unexpected result. E.g., Converting 50% to a decimal should yield 0.5; however, (/ 50 100) returns 0.

Tentative workaround pending an answer: (* PERCENT .01)

1 Answer 1

4

Your workaround tells the whole story:

(/ 1 2) ;=> 0
(/ 1.0 2) ;=>0.5

With integers, emacs does integer arithmetic, with floats it does floating-point arithmetic.


See the Elisp manual, node Arithmetic Operations:

Except for %, each of these functions accepts both integer and floating-point arguments, and returns a floating-point number if any argument is floating point.

And, for /:

If all the arguments are integers, the result is an integer, obtained by rounding the quotient towards zero after each division.

See also node Numeric Conversions, for info about explicitly converting among numeric types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.