Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string-to-number and insert give control characters (e.g., ^C, ^D) instead of numbers (e.g., 3, 4)

Tags:

emacs

lisp

elisp

I am trying to make a function that prompt the user to enter a number during a while (so the number of time the prompt appears will change, that is why I don't want to use something like (interactive "n Enter a Number : ")). My code is like this:

 (setq nb_str (read-string "Enter a Number:"))
 (setq nb (string-to-number nb_str))
 (insert nb)

When I want to insert the variable nb, instead of having my number inserted, I get some weird output from string-to-number function like ^C (if I typed 3) or ^D (if I typed 4) or ^E (if I typed 5). I need that value as number for after. How can I do this?

like image 996
Dotgreg Avatar asked Jan 30 '26 11:01

Dotgreg


1 Answers

insert inserts strings or characters, and a number in Emacs Lisp is a character (if it is in the right range), so 3 is inserted as the 3rd ASCII char, ^C.

You need to either pass nb-str to insert, or use number-to-string:

(insert (number-to-string nb))
like image 108
sds Avatar answered Feb 01 '26 18:02

sds