unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Format with %-*
Date: Tue, 09 Jul 2024 15:39:06 +0200	[thread overview]
Message-ID: <87r0c2lm6d.fsf@dataswamp.org> (raw)
In-Reply-To: 87msmreyap.fsf@web.de

Michael Heerdegen via Users list for the GNU Emacs text editor wrote:

>> How can I construct a format that uses a space spx followed
>> by a field of width 13 into which I insert a label lbv2.
>
> In Elisp we typically use `string-pad' and
> `truncate-string-to-width'.
>
> It's also possible to add padding with `format' though -
> like in
>
> (format "%10s" "test") -> "      test"

I have made a lot of stuff like the example below.

But never felt 100% about it ...

(defun faster (old new)
  (format "%.1f%%" (* 100 (1- (/ new old 1.0)))) )

;; (faster 0.2  0.5)  ; 150.0%
;; (faster 1.35 0.84) ; -37.8%
;; (faster  200  400) ; 100.0%
;; (faster 1000 1000) ;   0.0%
;; (faster 1000  100) ; -90.0%

(defun percent (nom &optional denom str)
  (or denom (setq denom 1))
  (let ((perc (/ nom denom 0.01)))
    (if str
        (format "%.1f%%" perc)
      perc)))

;; (percent 0.25)           ; 25.0
;; (percent 50 75)          ; 66.66 ...
;; (percent (// 1 2) nil t) ; 50.0%
;; (percent 1019 22 t)      ; 4631.8%

-- 
underground experts united
https://dataswamp.org/~incal




      reply	other threads:[~2024-07-09 13:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-08 16:07 Format with %-* Heime
2024-07-08 17:47 ` Bruno Barbier
2024-07-08 19:53   ` Heime
2024-07-09  8:07     ` Bruno Barbier
2024-07-09  8:59     ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-07-09 13:39       ` Emanuel Berg [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r0c2lm6d.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).