1

I am trying to output

Humidity: 42% 

using both functions print and message. While print has no problem with the literal percentage sign, message throws a "Format string ends in middle of format specifier". What am I missing?

(defun demo()
  (interactive)
  (progn
    (setq text (format "Humidity: %s%%" 42))
    (print text)
    (message text)
    ))
(global-set-key (kbd "<f5>") 'demo)
2

1 Answer 1

4

By running (message (format ...)), you're using the string as a format twice.

print takes an object as its parameter, while message takes a format string. Use "%s" as the format and you can specify the string as the second optional argument to message:

(setq text (format "Humidity %s%%" 42))
(print text)
(message "%s" text)
  ⇒ "Humidity 42%"

or don't run format on text before sending it to message:

(setq value 42)
(setq format "Humidity %s%%")
(print (format format value))
(message format value)
  ⇒ "Humidity 42%"
1
  • @NickD: The print line was missing in one of them.
    – choroba
    Commented Jun 21, 2021 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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